Convert btree locking to an rwsem
cover Convert btree locking to an rwsem
v1->v2:
- broke the lockdep fixes out into their own series.
- added a new fix where we need to handle double splitting leaves in some cases.
----- Original email -------
tl;dr:
- Cleaner code.
- dbench 200 3x as fast.
- fio with < nr_cpus is 10% slower.
- fio with 2 * nr_cpus is 10% faster.
- fs_mark is the same ish (results have a 10% variance between runs).
When Chris originally wrote the locking code for Btrfs the performance of rwsem was atroctios compared to his locking scheme. In btrfs we can usually spin on most accesses, because generally we won't need to block. If we do need to block we can simply flip over to a blocking lock. This flexibility has meant that our locking scheme generally outperforms any of the standard locking. We also have a unique usecase where in a specific case (the space cache) we need to be able to recurse a tree lock. It is safe to do this because we're just reading, and we would currently be holding a write lock, but this is not something that the generic locking infrastructure provides.
There are a few downsides
1) The code is complicated. This generally isn't an issue because we don't really have to touch the code that often.
2) We lose lockdep help. As you can see the first 4 patches in this series are fixing deadlocks that exist that were caught by having lockdep properly working with our tree locking.
3) We actually perform really poorly in the highly contended case. Because we don't really do anything proper locks do like dealing with writer/read starvation, we have a series of waitqueues that mass get woken up when we unlock blocking. This means our context switch counts can be stupid high, as we'll wake everybody up, and whoever wins the race gets the lock. Implementing proper exclusive waitqueues is a possiblity, but would further complicate the code, and likely introduce problems we'd spend the next 6 months figuring out.
It's #3 that actually brought me to this patch series, as changing to an rwsem significantly improved a usecase I was investigating. However the rwsem isn't without drawbacks. Namely that in less highly contended cases we can be slower than baseline, around 9-10% on my test box. However in the very highly contended cases (> nr_cpus) we are the same, if not better.
Filipe and I have discussed this a bit over the last few days, we're both on the fence about it currently. On one hand we could likely make up the lost performance in other areas, and there's certainly things that could be investigated to see why exactly we're slower with the rwsem and maybe figure out a way to fix those. We also gain a lot with this new code. But 9-10% isn't nothing, so it needs to be taken into consideration.
The following is the set of benchmarks that I've run on my 80 CPU, 256 GIB of ram, 2 TIB NVME drive machine. If there's anything else people would like to see I can easily do A/B testing to see.
fio was N threads with 1gib files, 64kib blocksize with a fsync after every write. The fs_mark was just create empty files with 16 threads.
PATCHED UNPATCHED % DIFF dbench 200 699.011 Mb/s 213.568 Mb/s +227% fs_mark 223 seconds 197 seconds -11.65% fio 64 threads 562 Mb/s 624 Mb/s -9.9% fio 100 threads 566 Mb/s 566 Mb/s 0.0% fio 160 threads 593 Mb/s 576 Mb/s +2.95%
Another thing not shown by the raw numbers is the number of context switches. Because of the nature of our locking we wake up everything constantly, so our context switch counts for the fio jobs are consistently 2-3x with the old scheme vs the rwsem. This is why my particular workload performed so poorly, the context switching was actually quite painful for that workload.
The diffstat is as follows
fs/btrfs/backref.c | 13 +- fs/btrfs/ctree.c | 172 +++++-------- fs/btrfs/ctree.h | 10 +- fs/btrfs/delayed-inode.c | 11 - fs/btrfs/dir-item.c | 1 - fs/btrfs/disk-io.c | 13 +- fs/btrfs/export.c | 1 - fs/btrfs/extent-tree.c | 39 +-- fs/btrfs/extent_io.c | 16 +- fs/btrfs/extent_io.h | 21 +- fs/btrfs/file-item.c | 4 - fs/btrfs/file.c | 3 +- fs/btrfs/free-space-tree.c | 2 - fs/btrfs/inode-item.c | 6 - fs/btrfs/inode.c | 13 +- fs/btrfs/ioctl.c | 4 +- fs/btrfs/locking.c | 469 ++++++---------------------------- fs/btrfs/locking.h | 92 ++++++- fs/btrfs/print-tree.c | 11 +- fs/btrfs/qgroup.c | 11 +- fs/btrfs/ref-verify.c | 6 +- fs/btrfs/reflink.c | 3 - fs/btrfs/relocation.c | 15 +- fs/btrfs/super.c | 2 - fs/btrfs/tests/qgroup-tests.c | 4 - fs/btrfs/transaction.c | 7 +- fs/btrfs/tree-defrag.c | 1 - fs/btrfs/tree-log.c | 3 - 28 files changed, 273 insertions(+), 680 deletions(-)
Thanks,
Josef
PATCH 1/12 btrfs: rename eb->lock_nested to eb->lock_recursed
Nested locking with lockdep and everything else refers to lock hierarchy within the same lock map. This is how we indicate the same locks for different objects are ok to take in a specific order, for our use case that would be to take the lock on a leaf and then take a lock on an adjacent leaf.
What ->lock_nested _actually_ refers to is if we happen to already be holding the write lock on the extent buffer and we're allowing a read lock to be taken on that extent buffer, which is recursion. Rename this so we don't get confused when we switch to a rwsem and have to start using the _nested helpers.
--- fs/btrfs/extent_io.c | 2 +- fs/btrfs/extent_io.h | 2 +- fs/btrfs/locking.c | 22 +++++++++++----------- 3 files changed, 13 insertions(+), 13 deletions(-)
fs/btrfs/extent_io.c +1 -1
@@ -4990,7 +4990,7 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,rwlock_init (&eb->lock);atomic_set (&eb->blocking_readers,0 ); eb->blocking_writers =0 ; - eb->lock_nested = false; + eb->lock_recursed = false;init_waitqueue_head (&eb->write_lock_wq);init_waitqueue_head (&eb->read_lock_wq);
fs/btrfs/extent_io.h +1 -1
@@ -102,7 +102,7 @@ struct extent_buffer {int blocking_writers;atomic_t blocking_readers; -bool lock_nested; +bool lock_recursed;/* >= 0 if eb belongs to a log tree, -1 otherwise */ short log_index;
fs/btrfs/locking.c +11 -11
@@ -57,7 +57,7 @@* performance reasons. * * -* Lock nesting +* Lock recursion * ------------ * * A write operation on a tree might indirectly start a look up on the same
@@ -201,7 +201,7 @@ void btrfs_set_lock_blocking_read(struct extent_buffer *eb)* lock, but it won't change to or away from us. If we have the write * lock, we are the owner and it'll never change. */ -if (eb->lock_nested && current->pid == eb->lock_owner) +if (eb->lock_recursed && current->pid == eb->lock_owner)return ;btrfs_assert_tree_read_locked (eb);atomic_inc (&eb->blocking_readers);
@@ -225,7 +225,7 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb)* lock, but it won't change to or away from us. If we have the write * lock, we are the owner and it'll never change. */ -if (eb->lock_nested && current->pid == eb->lock_owner) +if (eb->lock_recursed && current->pid == eb->lock_owner)return ;if (eb->blocking_writers ==0 ) {btrfs_assert_spinning_writers_put (eb);
@@ -263,8 +263,8 @@ void btrfs_tree_read_lock(struct extent_buffer *eb)* depends on this as it may be called on a partly * (write-)locked tree. */ -BUG_ON (eb->lock_nested); - eb->lock_nested = true; +BUG_ON (eb->lock_recursed); + eb->lock_recursed = true;read_unlock (&eb->lock);trace_btrfs_tree_read_lock (eb, start_ns);return ;
@@ -362,11 +362,11 @@ void btrfs_tree_read_unlock(struct extent_buffer *eb)/* * if we're nested, we have the write lock. No new locking * is needed as long as we are the lock owner. -* The write unlock will do a barrier for us, and the lock_nested +* The write unlock will do a barrier for us, and the lock_recursed * field only matters to the lock owner. */ -if (eb->lock_nested && current->pid == eb->lock_owner) { - eb->lock_nested = false; +if (eb->lock_recursed && current->pid == eb->lock_owner) { + eb->lock_recursed = false;return ; }btrfs_assert_tree_read_locked (eb);
@@ -388,11 +388,11 @@ void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)/* * if we're nested, we have the write lock. No new locking * is needed as long as we are the lock owner. -* The write unlock will do a barrier for us, and the lock_nested +* The write unlock will do a barrier for us, and the lock_recursed * field only matters to the lock owner. */ -if (eb->lock_nested && current->pid == eb->lock_owner) { - eb->lock_nested = false; +if (eb->lock_recursed && current->pid == eb->lock_owner) { + eb->lock_recursed = false;return ; }btrfs_assert_tree_read_locked (eb);
Signature · 1 line
2.24.1
PATCH 2/12 btrfs: introduce path->recurse
In reply to Josef BacikOur current tree locking stuff allows us to recurse with read locks if we're already holding the write lock. This is necessary for the space cache inode, as we could be holding a lock on the root_tree root when we need to cache a block group, and thus need to be able to read down the root_tree to read in the inode cache.
We can get away with this in our current locking, but we won't be able to with a rwsem. Handle this by purposefully annotating the places where we require recursion, so that in the future we can maybe come up with a way to avoid the recursion. In the case of the free space inode, this will be superseded by the free space tree.
--- fs/btrfs/ctree.c | 8 ++++---- fs/btrfs/ctree.h | 3 +-- fs/btrfs/inode.c | 2 ++ fs/btrfs/locking.c | 13 ++++++++++--- fs/btrfs/locking.h | 10 ++++++++++ 5 files changed, 27 insertions(+), 9 deletions(-)
fs/btrfs/ctree.c +4 -4
@@ -2601,7 +2601,7 @@ static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,* We don't know the level of the root node until we actually * have it read locked */ - b =btrfs_read_lock_root_node (root); + b =__btrfs_read_lock_root_node (root, p->recurse); level =btrfs_header_level (b);if (level > write_lock_level) goto out;
@@ -2875,7 +2875,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, }else {if (!btrfs_tree_read_lock_atomic (b)) {btrfs_set_path_blocking (p); -btrfs_tree_read_lock (b); +__btrfs_tree_read_lock (b, p->recurse); } p->locks[level] =BTRFS_READ_LOCK ; }
@@ -5379,7 +5379,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, }if (!ret) {btrfs_set_path_blocking (path); -btrfs_tree_read_lock (next); +__btrfs_tree_read_lock (next, path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
@@ -5414,7 +5414,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, ret =btrfs_try_tree_read_lock (next);if (!ret) {btrfs_set_path_blocking (path); -btrfs_tree_read_lock (next); +__btrfs_tree_read_lock (next, path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
fs/btrfs/ctree.h +1 -2
@@ -374,6 +374,7 @@ struct btrfs_path { unsigned int search_commit_root:1; unsigned int need_commit_sem:1; unsigned int skip_release_on_error:1; + unsigned int recurse:1; };#define BTRFS_MAX_EXTENT_ITEM_SIZE (r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \ sizeof(struct btrfs_item))
@@ -2654,8 +2655,6 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,struct btrfs_path *path, const struct btrfs_key *new_key);struct extent_buffer *btrfs_root_node (struct btrfs_root *root); -struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root); -struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root);int btrfs_find_next_key (struct btrfs_root *root,struct btrfs_path *path,struct btrfs_key *key,int lowest_level,u64 min_trans);
fs/btrfs/inode.c +2 -0
@@ -6592,6 +6592,8 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,*/ path->leave_spinning =1 ; + path->recurse =btrfs_is_free_space_inode (inode); + ret =btrfs_lookup_file_extent (NULL , root, path, objectid, start,0 );if (ret <0 ) { err = ret;
fs/btrfs/locking.c +10 -3
@@ -244,7 +244,7 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb)* * The rwlock is held upon exit. */ -void btrfs_tree_read_lock (struct extent_buffer *eb) +void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse) {u64 start_ns =0 ;
@@ -263,6 +263,7 @@ void btrfs_tree_read_lock(struct extent_buffer *eb)* depends on this as it may be called on a partly * (write-)locked tree. */ +WARN_ON (!recurse);BUG_ON (eb->lock_recursed); eb->lock_recursed = true;read_unlock (&eb->lock);
@@ -279,6 +280,11 @@ void btrfs_tree_read_lock(struct extent_buffer *eb)trace_btrfs_tree_read_lock (eb, start_ns); } +void btrfs_tree_read_lock (struct extent_buffer *eb) +{ +__btrfs_tree_read_lock (eb, false); +} +/* * Lock extent buffer for read, optimistically expecting that there are no * contending blocking writers. If there are, don't wait.
@@ -552,13 +558,14 @@ struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)* * Return: root extent buffer with read lock held */ -struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root) +struct extent_buffer *__btrfs_read_lock_root_node (struct btrfs_root *root, +bool recurse) {struct extent_buffer *eb;while (1 ) { eb =btrfs_root_node (root); -btrfs_tree_read_lock (eb); +__btrfs_tree_read_lock (eb, recurse);if (eb == root->node)break ;btrfs_tree_read_unlock (eb);
fs/btrfs/locking.h +10 -0
@@ -21,6 +21,7 @@ struct btrfs_path;void btrfs_tree_lock (struct extent_buffer *eb);void btrfs_tree_unlock (struct extent_buffer *eb); +void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse);void btrfs_tree_read_lock (struct extent_buffer *eb);void btrfs_tree_read_unlock (struct extent_buffer *eb);void btrfs_tree_read_unlock_blocking (struct extent_buffer *eb);
@@ -29,6 +30,15 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);int btrfs_try_tree_write_lock (struct extent_buffer *eb);int btrfs_tree_read_lock_atomic (struct extent_buffer *eb); +struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root); +struct extent_buffer *__btrfs_read_lock_root_node (struct btrfs_root *root, +bool recurse); + +static inline struct extent_buffer * +btrfs_read_lock_root_node (struct btrfs_root *root) +{ +return __btrfs_read_lock_root_node (root, false); +}#ifdef CONFIG_BTRFS_DEBUG static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) {
Signature · 1 line
2.24.1
PATCH 3/12 btrfs: add nesting tags to the locking helpers
In reply to Josef BacikWe will need these when we switch to an rwsem, so plumb in the infrastructure here to use later on. I violate the 80 character limit some here because it'll be cleaned up later.
--- fs/btrfs/ctree.c | 11 ++++++++--- fs/btrfs/locking.c | 14 ++++++++++---- fs/btrfs/locking.h | 20 +++++++++++++++++++- 3 files changed, 37 insertions(+), 8 deletions(-)
fs/btrfs/ctree.c +8 -3
@@ -2875,7 +2875,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, }else {if (!btrfs_tree_read_lock_atomic (b)) {btrfs_set_path_blocking (p); -__btrfs_tree_read_lock (b, p->recurse); +__btrfs_tree_read_lock (b,BTRFS_NESTING_NORMAL , + p->recurse); } p->locks[level] =BTRFS_READ_LOCK ; }
@@ -5379,7 +5380,9 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, }if (!ret) {btrfs_set_path_blocking (path); -__btrfs_tree_read_lock (next, path->recurse); +__btrfs_tree_read_lock (next, +BTRFS_NESTING_NORMAL , + path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
@@ -5414,7 +5417,9 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, ret =btrfs_try_tree_read_lock (next);if (!ret) {btrfs_set_path_blocking (path); -__btrfs_tree_read_lock (next, path->recurse); +__btrfs_tree_read_lock (next, +BTRFS_NESTING_NORMAL , + path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
fs/btrfs/locking.c +10 -4
@@ -244,7 +244,8 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb)* * The rwlock is held upon exit. */ -void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse) +void __btrfs_tree_read_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest, +bool recurse) {u64 start_ns =0 ;
@@ -282,7 +283,7 @@ void __btrfs_tree_read_lock(struct extent_buffer *eb, bool recurse)void btrfs_tree_read_lock (struct extent_buffer *eb) { -__btrfs_tree_read_lock (eb, false); +__btrfs_tree_read_lock (eb,BTRFS_NESTING_NORMAL , false); }/*
@@ -415,7 +416,7 @@ void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)* * The rwlock is held for write upon exit. */ -void btrfs_tree_lock (struct extent_buffer *eb) +void __btrfs_tree_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest) __acquires(&eb->lock) {u64 start_ns =0 ;
@@ -440,6 +441,11 @@ void btrfs_tree_lock(struct extent_buffer *eb)trace_btrfs_tree_lock (eb, start_ns); } +void btrfs_tree_lock (struct extent_buffer *eb) +{ +__btrfs_tree_lock (eb,BTRFS_NESTING_NORMAL ); +} +/* * Release the write lock, either blocking or spinning (ie. there's no need * for an explicit blocking unlock, like btrfs_tree_read_unlock_blocking).
@@ -565,7 +571,7 @@ struct extent_buffer *__btrfs_read_lock_root_node(struct btrfs_root *root,while (1 ) { eb =btrfs_root_node (root); -__btrfs_tree_read_lock (eb, recurse); +__btrfs_tree_read_lock (eb,BTRFS_NESTING_NORMAL , recurse);if (eb == root->node)break ;btrfs_tree_read_unlock (eb);
fs/btrfs/locking.h +19 -1
@@ -16,12 +16,30 @@#define BTRFS_WRITE_LOCK_BLOCKING 3#define BTRFS_READ_LOCK_BLOCKING 4 +enum btrfs_lock_nesting { +BTRFS_NESTING_NORMAL , + +/* +* We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so +* add this in here and add a static_assert to keep us from going over +* the limit. As of this writing we're limited to 8, and we're +* definitely using 8, hence this check to keep us from messing up in +* the future. +*/ +BTRFS_NESTING_MAX , +}; + +static_assert (BTRFS_NESTING_MAX <=MAX_LOCKDEP_SUBCLASSES , +"too many lock subclasses defined" ); +struct btrfs_path ; +void __btrfs_tree_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest);void btrfs_tree_lock (struct extent_buffer *eb);void btrfs_tree_unlock (struct extent_buffer *eb); -void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse); +void __btrfs_tree_read_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest, +bool recurse);void btrfs_tree_read_lock (struct extent_buffer *eb);void btrfs_tree_read_unlock (struct extent_buffer *eb);void btrfs_tree_read_unlock_blocking (struct extent_buffer *eb);
Signature · 1 line
2.24.1
PATCH 4/12 btrfs: introduce BTRFS_NESTING_COW for cow'ing blocks
In reply to Josef BacikWhen we COW a block we are holding a lock on the original block, and then we lock the new COW block. Because our lockdep maps are based on root + level, this will make lockdep complain. We need a way to indicate a subclass for locking the COW'ed block, so plumb through our btrfs_lock_nesting from btrfs_cow_block down to the btrfs_init_buffer, and then introduce BTRFS_NESTING_COW to be used for cow'ing blocks.
The reason I've added all this extra infrastructure is because there will be need of different nesting classes in follow up patches.
--- fs/btrfs/ctree.c | 52 ++++++++++++++++++++++++++---------------- fs/btrfs/ctree.h | 6 +++-- fs/btrfs/disk-io.c | 5 ++-- fs/btrfs/extent-tree.c | 12 ++++++---- fs/btrfs/ioctl.c | 3 ++- fs/btrfs/locking.h | 8 +++++++ fs/btrfs/relocation.c | 11 +++++---- fs/btrfs/transaction.c | 5 ++-- 8 files changed, 66 insertions(+), 36 deletions(-)
fs/btrfs/ctree.c +32 -20
@@ -198,7 +198,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,btrfs_node_key (buf, &disk_key,0 ); cow =btrfs_alloc_tree_block (trans, root,0 , new_root_objectid, - &disk_key, level, buf->start,0 ); + &disk_key, level, buf->start,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (cow))return PTR_ERR (cow);
@@ -957,7 +957,8 @@ static struct extent_buffer *alloc_tree_block_no_bg_flush(const struct btrfs_disk_key *disk_key, int level, u64 hint, - u64 empty_size) + u64 empty_size, + enum btrfs_lock_nesting nest) { struct btrfs_fs_info *fs_info = root->fs_info;struct extent_buffer *ret;
@@ -986,7 +987,7 @@ static struct extent_buffer *alloc_tree_block_no_bg_flush( ret =btrfs_alloc_tree_block (trans, root, parent_start, root->root_key.objectid, disk_key, level, - hint, empty_size); + hint, empty_size, nest); trans->can_flush_pending_bgs = true;return ret;
@@ -1009,7 +1010,8 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,struct extent_buffer *buf, struct extent_buffer *parent, int parent_slot, struct extent_buffer **cow_ret, - u64 search_start, u64 empty_size) + u64 search_start, u64 empty_size, + enum btrfs_lock_nesting nest) { struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_disk_key disk_key;
@@ -1040,7 +1042,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, parent_start = parent->start; cow =alloc_tree_block_no_bg_flush (trans, root, parent_start, &disk_key, - level, search_start, empty_size); + level, search_start, empty_size, nest);if (IS_ERR (cow))return PTR_ERR (cow);
@@ -1446,7 +1448,8 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans, noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,struct btrfs_root *root,struct extent_buffer *buf,struct extent_buffer *parent,int parent_slot, -struct extent_buffer **cow_ret) +struct extent_buffer **cow_ret, +enum btrfs_lock_nesting nest) {struct btrfs_fs_info *fs_info = root->fs_info;u64 search_start;
@@ -1485,7 +1488,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,*/ btrfs_qgroup_trace_subtree_after_cow (trans, root, buf); ret =__btrfs_cow_block (trans, root, buf, parent, - parent_slot, cow_ret, search_start,0 ); + parent_slot, cow_ret, search_start,0 , nest);trace_btrfs_cow_block (root, buf, *cow_ret);
@@ -1657,7 +1660,8 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, err =__btrfs_cow_block (trans, root, cur, parent, i, &cur, search_start,min (16 * blocksize, - (end_slot - i) * blocksize)); + (end_slot - i) * blocksize), +BTRFS_NESTING_COW );if (err) {btrfs_tree_unlock (cur);free_extent_buffer (cur);
@@ -1855,7 +1859,8 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_tree_lock (child);btrfs_set_lock_blocking_write (child); - ret =btrfs_cow_block (trans, root, child, mid,0 , &child); + ret =btrfs_cow_block (trans, root, child, mid,0 , &child, +BTRFS_NESTING_COW );if (ret) {btrfs_tree_unlock (child);free_extent_buffer (child);
@@ -1894,7 +1899,8 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_tree_lock (left);btrfs_set_lock_blocking_write (left); wret =btrfs_cow_block (trans, root, left, - parent, pslot -1 , &left); + parent, pslot -1 , &left, +BTRFS_NESTING_COW );if (wret) { ret = wret; goto enospc;
@@ -1909,7 +1915,8 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_tree_lock (right);btrfs_set_lock_blocking_write (right); wret =btrfs_cow_block (trans, root, right, - parent, pslot +1 , &right); + parent, pslot +1 , &right, +BTRFS_NESTING_COW );if (wret) { ret = wret; goto enospc;
@@ -2077,7 +2084,8 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, wret =1 ; }else { ret =btrfs_cow_block (trans, root, left, parent, - pslot -1 , &left); + pslot -1 , &left, +BTRFS_NESTING_COW );if (ret) wret =1 ;else {
@@ -2132,7 +2140,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, }else { ret =btrfs_cow_block (trans, root, right, parent, pslot +1 , - &right); + &right,BTRFS_NESTING_COW );if (ret) wret =1 ;else {
@@ -2740,11 +2748,13 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,btrfs_set_path_blocking (p);if (last_level) err =btrfs_cow_block (trans, root, b,NULL ,0 , - &b); + &b, +BTRFS_NESTING_COW );else err =btrfs_cow_block (trans, root, b, p->nodes[level +1 ], - p->slots[level +1 ], &b); + p->slots[level +1 ], &b, +BTRFS_NESTING_COW );if (err) { ret = err; goto done;
@@ -3332,7 +3342,8 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,btrfs_node_key (lower, &lower_key,0 ); c =alloc_tree_block_no_bg_flush (trans, root,0 , &lower_key, level, - root->node->start,0 ); + root->node->start,0 , +BTRFS_NESTING_NORMAL );if (IS_ERR (c))return PTR_ERR (c);
@@ -3462,7 +3473,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,btrfs_node_key (c, &disk_key, mid); split =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key, level, - c->start,0 ); + c->start,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (split))return PTR_ERR (split);
@@ -3740,7 +3751,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, right, upper, - slot +1 , &right); + slot +1 , &right,BTRFS_NESTING_COW );if (ret) goto out_unlock;
@@ -3975,7 +3986,8 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, left, - path->nodes[1 ], slot -1 , &left); + path->nodes[1 ], slot -1 , &left, +BTRFS_NESTING_COW );if (ret) {/* we hit -ENOSPC, but it isn't fatal here */ if (ret == -ENOSPC )
@@ -4238,7 +4250,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,btrfs_item_key (l, &disk_key, mid); right =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key,0 , - l->start,0 ); + l->start,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (right))return PTR_ERR (right);
fs/btrfs/ctree.h +4 -2
@@ -2526,7 +2526,8 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, u64 parent, u64 root_objectid, const struct btrfs_disk_key *key, int level, u64 hint, - u64 empty_size); + u64 empty_size, + enum btrfs_lock_nesting nest); void btrfs_free_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct extent_buffer *buf,
@@ -2667,7 +2668,8 @@ struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,int btrfs_cow_block (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct extent_buffer *buf,struct extent_buffer *parent,int parent_slot, -struct extent_buffer **cow_ret); +struct extent_buffer **cow_ret, +enum btrfs_lock_nesting nest); int btrfs_copy_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct extent_buffer *buf,
fs/btrfs/disk-io.c +3 -2
@@ -1209,7 +1209,8 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, root->root_key.type =BTRFS_ROOT_ITEM_KEY ; root->root_key.offset =0 ; - leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 ); + leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 , +BTRFS_NESTING_NORMAL );if (IS_ERR (leaf)) { ret =PTR_ERR (leaf); leaf =NULL ;
@@ -1281,7 +1282,7 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,*/ leaf =btrfs_alloc_tree_block (trans, root,0 ,BTRFS_TREE_LOG_OBJECTID , -NULL ,0 ,0 ,0 ); +NULL ,0 ,0 ,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (leaf)) {btrfs_put_root (root);return ERR_CAST (leaf);
fs/btrfs/extent-tree.c +7 -5
@@ -4500,7 +4500,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,static struct extent_buffer *btrfs_init_new_buffer (struct btrfs_trans_handle *trans,struct btrfs_root *root, -u64 bytenr,int level,u64 owner) +u64 bytenr,int level,u64 owner, +enum btrfs_lock_nesting nest) {struct btrfs_fs_info *fs_info = root->fs_info;struct extent_buffer *buf;
@@ -4523,7 +4524,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, }btrfs_set_buffer_lockdep_class (owner, buf, level); -btrfs_tree_lock (buf); +__btrfs_tree_lock (buf, nest);btrfs_clean_tree_block (buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags);
@@ -4569,7 +4570,8 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, u64 parent, u64 root_objectid, const struct btrfs_disk_key *key, int level, u64 hint, - u64 empty_size) + u64 empty_size, + enum btrfs_lock_nesting nest) { struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_key ins;
@@ -4585,7 +4587,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS if (btrfs_is_testing (fs_info)) { buf =btrfs_init_new_buffer (trans, root, root->alloc_bytenr, - level, root_objectid); + level, root_objectid, nest);if (!IS_ERR (buf)) root->alloc_bytenr += blocksize;return buf;
@@ -4602,7 +4604,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, goto out_unuse; buf =btrfs_init_new_buffer (trans, root, ins.objectid, level, - root_objectid); + root_objectid, nest);if (IS_ERR (buf)) { ret =PTR_ERR (buf); goto out_free_reserved;
fs/btrfs/ioctl.c +2 -1
@@ -628,7 +628,8 @@ static noinline int create_subvol(struct inode *dir,if (ret) goto fail; - leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 ); + leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 , +BTRFS_NESTING_NORMAL );if (IS_ERR (leaf)) { ret =PTR_ERR (leaf); goto fail;
fs/btrfs/locking.h +8 -0
@@ -19,6 +19,14 @@enum btrfs_lock_nesting {BTRFS_NESTING_NORMAL , +/* +* When we COW a block we are holding the lock on the original block, +* and since our lockdep maps are rootid+level, this confuses lockdep +* when we lock the newly allocated COW'd block. Handle this by having +* a subclass for COW'ed blocks so that lockdep doesn't complain. +*/ +BTRFS_NESTING_COW , +/* * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so * add this in here and add a static_assert to keep us from going over
fs/btrfs/relocation.c +7 -4
@@ -1206,7 +1206,8 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, }if (cow) { - ret =btrfs_cow_block (trans, dest, eb,NULL ,0 , &eb); + ret =btrfs_cow_block (trans, dest, eb,NULL ,0 , &eb, +BTRFS_NESTING_COW );BUG_ON (ret); }btrfs_set_lock_blocking_write (eb);
@@ -1274,7 +1275,8 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,btrfs_tree_lock (eb);if (cow) { ret =btrfs_cow_block (trans, dest, eb, parent, - slot, &eb); + slot, &eb, +BTRFS_NESTING_COW );BUG_ON (ret); }btrfs_set_lock_blocking_write (eb);
@@ -1781,7 +1783,8 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,* relocated and the block is tree root. */ leaf =btrfs_lock_root_node (root); - ret =btrfs_cow_block (trans, root, leaf,NULL ,0 , &leaf); + ret =btrfs_cow_block (trans, root, leaf,NULL ,0 , &leaf, +BTRFS_NESTING_COW );btrfs_tree_unlock (leaf);free_extent_buffer (leaf);if (ret <0 )
@@ -2308,7 +2311,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,if (!node->eb) { ret =btrfs_cow_block (trans, root, eb, upper->eb, - slot, &eb); + slot, &eb,BTRFS_NESTING_COW );btrfs_tree_unlock (eb);free_extent_buffer (eb);if (ret <0 ) {
fs/btrfs/transaction.c +3 -2
@@ -1182,7 +1182,7 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) eb =btrfs_lock_root_node (fs_info->tree_root); ret =btrfs_cow_block (trans, fs_info->tree_root, eb,NULL , -0 , &eb); +0 , &eb,BTRFS_NESTING_COW );btrfs_tree_unlock (eb);free_extent_buffer (eb);
@@ -1587,7 +1587,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,btrfs_set_root_otransid (new_root_item, trans->transid); old =btrfs_lock_root_node (root); - ret =btrfs_cow_block (trans, root, old,NULL ,0 , &old); + ret =btrfs_cow_block (trans, root, old,NULL ,0 , &old, +BTRFS_NESTING_COW );if (ret) {btrfs_tree_unlock (old);free_extent_buffer (old);
Signature · 1 line
2.24.1
PATCH 5/12 btrfs: introduce BTRFS_NESTING_LEFT/BTRFS_NESTING_RIGHT
In reply to Josef BacikOur lockdep maps are based on rootid+level, however in some cases we will lock adjacent blocks on the same level, namely in searching forward or in split/balance. Because of this lockdep will complain, so we need a separate subclass to indicate to lockdep that these are different locks.
lock leaf -> BTRFS_NESTING_NORMAL
cow leaf -> BTRFS_NESTING_COW
split leaf
lock left -> BTRFS_NESTING_LEFT
lock right -> BTRFS_NESTING_RIGHTThe above graph illustrates the need for this new nesting subclass.
--- fs/btrfs/ctree.c | 16 ++++++++-------- fs/btrfs/locking.h | 12 ++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-)
fs/btrfs/ctree.c +8 -8
@@ -1896,7 +1896,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, left =NULL ;if (left) { -btrfs_tree_lock (left); +__btrfs_tree_lock (left,BTRFS_NESTING_LEFT );btrfs_set_lock_blocking_write (left); wret = btrfs_cow_block(trans, root, left, parent, pslot - 1, &left,
@@ -1912,7 +1912,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, right =NULL ;if (right) { -btrfs_tree_lock (right); +__btrfs_tree_lock (right,BTRFS_NESTING_RIGHT );btrfs_set_lock_blocking_write (right); wret = btrfs_cow_block(trans, root, right, parent, pslot + 1, &right,
@@ -2076,7 +2076,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,if (left) {u32 left_nr; -btrfs_tree_lock (left); +__btrfs_tree_lock (left,BTRFS_NESTING_LEFT );btrfs_set_lock_blocking_write (left); left_nr =btrfs_header_nritems (left);
@@ -2131,7 +2131,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,if (right) {u32 right_nr; -btrfs_tree_lock (right); +__btrfs_tree_lock (right,BTRFS_NESTING_RIGHT );btrfs_set_lock_blocking_write (right); right_nr =btrfs_header_nritems (right);
@@ -3742,7 +3742,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_rootif (IS_ERR (right))return 1 ; -btrfs_tree_lock (right); +__btrfs_tree_lock (right,BTRFS_NESTING_RIGHT );btrfs_set_lock_blocking_write (right); free_space =btrfs_leaf_free_space (right);
@@ -3975,7 +3975,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_rootif (IS_ERR (left))return 1 ; -btrfs_tree_lock (left); +__btrfs_tree_lock (left,BTRFS_NESTING_LEFT );btrfs_set_lock_blocking_write (left); free_space =btrfs_leaf_free_space (left);
@@ -5393,7 +5393,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,if (!ret) {btrfs_set_path_blocking (path);__btrfs_tree_read_lock (next, -BTRFS_NESTING_NORMAL , +BTRFS_NESTING_RIGHT , path->recurse); } next_rw_lock =BTRFS_READ_LOCK ;
@@ -5430,7 +5430,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,if (!ret) {btrfs_set_path_blocking (path);__btrfs_tree_read_lock (next, -BTRFS_NESTING_NORMAL , +BTRFS_NESTING_RIGHT , path->recurse); } next_rw_lock =BTRFS_READ_LOCK ;
fs/btrfs/locking.h +12 -0
@@ -27,6 +27,18 @@ enum btrfs_lock_nesting {*/ BTRFS_NESTING_COW , +/* +* Oftentimes we need to lock adjacent nodes on the same level while +* still holding the lock on the original node we searched to, such as +* for searching forward or for split/balance. +* +* Because of this we need to indicate to lockdep that this is +* acceptable by having a different subclass for each of these +* operations. +*/ +BTRFS_NESTING_LEFT , +BTRFS_NESTING_RIGHT , +/* * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so * add this in here and add a static_assert to keep us from going over
Signature · 1 line
2.24.1
PATCH 6/12 btrfs: introduce BTRFS_NESTING_LEFT/RIGHT_COW
In reply to Josef BacikFor similar reasons as BTRFS_NESTING_COW, we need BTRFS_NESTING_LEFT/RIGHT_COW. The pattern is this
lock leaf -> BTRFS_NESTING_NORMAL
cow leaf -> BTRFS_NESTING_COW
split leaf
lock left -> BTRFS_NESTING_LEFT
cow left -> BTRFS_NESTING_LEFT_COWWe need this in order to indicate to lockdep that these locks are discrete and are being taken in a safe order.
--- fs/btrfs/ctree.c | 12 ++++++------ fs/btrfs/locking.h | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-)
fs/btrfs/ctree.c +6 -6
@@ -1900,7 +1900,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_set_lock_blocking_write (left); wret =btrfs_cow_block (trans, root, left, parent, pslot -1 , &left, -BTRFS_NESTING_COW ); +BTRFS_NESTING_LEFT_COW );if (wret) { ret = wret; goto enospc;
@@ -1916,7 +1916,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_set_lock_blocking_write (right); wret =btrfs_cow_block (trans, root, right, parent, pslot +1 , &right, -BTRFS_NESTING_COW ); +BTRFS_NESTING_RIGHT_COW );if (wret) { ret = wret; goto enospc;
@@ -2085,7 +2085,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, }else { ret =btrfs_cow_block (trans, root, left, parent, pslot -1 , &left, -BTRFS_NESTING_COW ); +BTRFS_NESTING_LEFT_COW );if (ret) wret =1 ;else {
@@ -2140,7 +2140,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, }else { ret =btrfs_cow_block (trans, root, right, parent, pslot +1 , - &right,BTRFS_NESTING_COW ); + &right,BTRFS_NESTING_RIGHT_COW );if (ret) wret =1 ;else {
@@ -3751,7 +3751,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, right, upper, - slot +1 , &right,BTRFS_NESTING_COW ); + slot +1 , &right,BTRFS_NESTING_RIGHT_COW );if (ret) goto out_unlock;
@@ -3987,7 +3987,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, left, path->nodes[1 ], slot -1 , &left, -BTRFS_NESTING_COW ); +BTRFS_NESTING_LEFT_COW );if (ret) {/* we hit -ENOSPC, but it isn't fatal here */ if (ret == -ENOSPC )
fs/btrfs/locking.h +8 -0
@@ -39,6 +39,14 @@ enum btrfs_lock_nesting {BTRFS_NESTING_LEFT ,BTRFS_NESTING_RIGHT , +/* +* When splitting we will be holding a lock on the left/right node when +* we need to cow that node, thus we need a new set of subclasses for +* these two operations. +*/ +BTRFS_NESTING_LEFT_COW , +BTRFS_NESTING_RIGHT_COW , +/* * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so * add this in here and add a static_assert to keep us from going over
Signature · 1 line
2.24.1
PATCH 7/12 btrfs: introduce BTRFS_NESTING_SPLIT for split blocks
In reply to Josef BacikIf we are splitting a leaf/node, we could do something like the following
lock(leaf) BTRFS_NESTING_NORMAL
lock(left) BTRFS_NESTING_LEFT + BTRFS_NESTING_COW
push from leaf -> left
reset path to point to left
split left
allocate new block, lock block BTRFS_NESTING_SPLIT
at the new block point we need to have a different nesting level, because we have already used either BTRFS_NESTING_LEFT or BTRFS_NESTING_RIGHT when pushing items from the original leaf into the adjacent leaves.
--- fs/btrfs/ctree.c | 4 ++-- fs/btrfs/locking.h | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +2 -2
@@ -3473,7 +3473,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,btrfs_node_key (c, &disk_key, mid); split =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key, level, - c->start,0 ,BTRFS_NESTING_NORMAL ); + c->start,0 ,BTRFS_NESTING_SPLIT );if (IS_ERR (split))return PTR_ERR (split);
@@ -4250,7 +4250,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,btrfs_item_key (l, &disk_key, mid); right =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key,0 , - l->start,0 ,BTRFS_NESTING_NORMAL ); + l->start,0 ,BTRFS_NESTING_SPLIT );if (IS_ERR (right))return PTR_ERR (right);
fs/btrfs/locking.h +9 -0
@@ -47,6 +47,15 @@ enum btrfs_lock_nesting {BTRFS_NESTING_LEFT_COW ,BTRFS_NESTING_RIGHT_COW , +/* +* When splitting we may push nodes to the left or right, but still use +* the subsequent nodes in our path, keeping our locks on those adjacent +* blocks. Thus when we go to allocate a new split block we've already +* used up all of our available subclasses, so this subclass exists to +* handle this case where we need to allocate a new split block. +*/ +BTRFS_NESTING_SPLIT , +/* * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so * add this in here and add a static_assert to keep us from going over
Signature · 1 line
2.24.1
PATCH 8/12 btrfs: introduce BTRFS_NESTING_NEW_ROOT for adding new roots
In reply to Josef BacikThe way we add new roots is confusing from a locking perspective for lockdep. We generally have the rule that we lock things in order from highest level to lowest, but in the case of adding a new level to the tree we actually allocate a new block for the root, which makes the locking go in reverse. A similar issue exists for snapshotting, we cow the original root for the root of a new tree, however they're at the same level. Address this by using BTRFS_NESTING_NEW_ROOT for these operations.
--- fs/btrfs/ctree.c | 5 +++-- fs/btrfs/locking.h | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +3 -2
@@ -198,7 +198,8 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,btrfs_node_key (buf, &disk_key,0 ); cow =btrfs_alloc_tree_block (trans, root,0 , new_root_objectid, - &disk_key, level, buf->start,0 ,BTRFS_NESTING_NORMAL ); + &disk_key, level, buf->start,0 , +BTRFS_NESTING_NEW_ROOT );if (IS_ERR (cow))return PTR_ERR (cow);
@@ -3343,7 +3344,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, c =alloc_tree_block_no_bg_flush (trans, root,0 , &lower_key, level, root->node->start,0 , -BTRFS_NESTING_NORMAL ); +BTRFS_NESTING_NEW_ROOT );if (IS_ERR (c))return PTR_ERR (c);
fs/btrfs/locking.h +14 -0
@@ -16,6 +16,11 @@#define BTRFS_WRITE_LOCK_BLOCKING 3#define BTRFS_READ_LOCK_BLOCKING 4 +/* +* We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at +* the time of this patch is 8, which is how many we're using here. Keep this +* in mind if you decide you want to add another subclass. +*/ enum btrfs_lock_nesting {BTRFS_NESTING_NORMAL ,
@@ -56,6 +61,15 @@ enum btrfs_lock_nesting {*/ BTRFS_NESTING_SPLIT , +/* +* When promoting a new block to a root we need to have a special +* subclass so we don't confuse lockdep, as it will appear that we are +* locking a higher level node before a lower level one. Copying also +* has this problem as it appears we're locking the same block again +* when we make a snapshot of an existing root. +*/ + BTRFS_NESTING_NEW_ROOT, +/* * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so * add this in here and add a static_assert to keep us from going over
Signature · 1 line
2.24.1
PATCH 9/12 btrfs: use BTRFS_NESTED_NEW_ROOT for double splits
In reply to Josef BacikI've made this change separate since it requires both of the newly added NESTED flags and I didn't want to slip it into one of those changes.
If we do a double split of a node we can end up doing a BTRFS_NESTED_SPLIT on level 0, which throws lockdep off because it appears as a double lock. Since we're maxed out on subclasses, use BTRFS_NESTED_NEW_ROOT if we had to do a double split. This is OK because we won't have to do a double split if we had to insert a new root, and the new root would be at a higher level anyway.
--- fs/btrfs/ctree.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
fs/btrfs/ctree.c +11 -1
@@ -4250,8 +4250,18 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,else btrfs_item_key (l, &disk_key, mid); +/* +* We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double +* split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES +* subclasses, which is 8 at the time of this patch, and we've maxed it +* out. In the future we could add a +* BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just +* use BTRFS_NESTING_NEW_ROOT. +*/ right =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key,0 , - l->start,0 ,BTRFS_NESTING_SPLIT ); + l->start,0 , num_doubles ? +BTRFS_NESTING_NEW_ROOT : +BTRFS_NESTING_SPLIT );if (IS_ERR (right))return PTR_ERR (right);
Signature · 1 line
2.24.1
PATCH 10/12 btrfs: change our extent buffer lock to a rw_semaphore
In reply to Josef BacikHistorically we've implemented our own locking because we wanted to be able to selectively spin or sleep based on what we were doing in the tree. For instance, if all of our nodes were in cache then there's rarely a reason to need to sleep waiting for node locks, as they'll likely become available soon. At the time this code was written the rw_semaphore didn't do adaptive spinning, and thus was orders of magnitude slower than our home grown locking.
However now the opposite is the case. There are a few problems with how we implement blocking locks, namely that we use a normal waitqueue and simply wake everybody up in reverse sleep order. This leads to some suboptimal performance behavior, and a lot of context switches in highly contended cases. rw_semaphores actually do this properly, and also have adaptive spinning that works relatively well.
The locking code is also a bit of a bear to understand, and we lose the benefit of lockdep for the most part because the blocking states of the lock are simply ad-hoc and not mapped into lockdep.
So rework the locking code to drop all of this custom locking stuff, and simply use a rw_semaphore for everything. This makes the locking much simpler for everything, as we can now drop a lot of cruft and blocking transitions. The performance numbers vary depending on the workload, because generally speaking there doesn't tend to be a lot of contention on the btree. However on my test system which is an 80 core single socket system with 256gib of RAM and a 2tib NVME drive I get the following results (with all debug options off)
dbench 200 baseline
Throughput 216.056 MB/sec 200 clients 200 procs max_latency=1471.197 ms
dbench 200 with patch
Throughput 737.188 MB/sec 200 clients 200 procs max_latency=714.346 ms
Previously we also used fs_mark to test this sort of contention, and those results are far less impressive, mostly because there's not enough tasks to really stress the locking
fs_mark -d /d[0-15] -S 0 -L 20 -n 100000 -s 0 -t 16
baseline
verage Files/sec: 160166.7
p50 Files/sec: 165832
p90 Files/sec: 123886
p99 Files/sec: 123495
real 3m26.527s user 2m19.223s sys 48m21.856s
patched
Average Files/sec: 164135.7
p50 Files/sec: 171095
p90 Files/sec: 122889
p99 Files/sec: 113819
real 3m29.660s user 2m19.990s sys 44m12.259s
--- fs/btrfs/extent_io.c | 13 +- fs/btrfs/extent_io.h | 21 +-- fs/btrfs/locking.c | 378 +++++++----------------------------------- fs/btrfs/locking.h | 2 +- fs/btrfs/print-tree.c | 11 +- 5 files changed, 67 insertions(+), 358 deletions(-)
fs/btrfs/extent_io.c +1 -12
@@ -4987,12 +4987,8 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, eb->len = len; eb->fs_info = fs_info; eb->bflags =0 ; -rwlock_init (&eb->lock); -atomic_set (&eb->blocking_readers,0 ); - eb->blocking_writers =0 ; +init_rwsem (&eb->lock); eb->lock_recursed = false; -init_waitqueue_head (&eb->write_lock_wq); -init_waitqueue_head (&eb->read_lock_wq);btrfs_leak_debug_add (&fs_info->eb_leak_lock, &eb->leak_list, &fs_info->allocated_ebs);
@@ -5008,13 +5004,6 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, > MAX_INLINE_EXTENT_BUFFER_SIZE);BUG_ON (len >MAX_INLINE_EXTENT_BUFFER_SIZE ); -#ifdef CONFIG_BTRFS_DEBUG - eb->spinning_writers =0 ; -atomic_set (&eb->spinning_readers,0 ); -atomic_set (&eb->read_locks,0 ); - eb->write_locks =0 ; -#endif -return eb; }
fs/btrfs/extent_io.h +2 -19
@@ -99,31 +99,14 @@ struct extent_buffer {int read_mirror;struct rcu_head rcu_head;pid_t lock_owner; - -int blocking_writers; -atomic_t blocking_readers;bool lock_recursed; +struct rw_semaphore lock; +/* >= 0 if eb belongs to a log tree, -1 otherwise */ short log_index; -/* protects write locks */ -rwlock_t lock; - -/* readers use lock_wq while they wait for the write -* lock holders to unlock -*/ -wait_queue_head_t write_lock_wq; - -/* writers use read_lock_wq while they wait for readers -* to unlock -*/ -wait_queue_head_t read_lock_wq;struct page *pages[INLINE_EXTENT_BUFFER_PAGES ];#ifdef CONFIG_BTRFS_DEBUG -int spinning_writers; -atomic_t spinning_readers; -atomic_t read_locks; -int write_locks;struct list_head leak_list;#endif };
fs/btrfs/locking.c +61 -317
477 lines not on this page · raw message
fs/btrfs/locking.h +1 -1
@@ -111,7 +111,7 @@ btrfs_read_lock_root_node(struct btrfs_root *root)#ifdef CONFIG_BTRFS_DEBUG static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) { -BUG_ON (!eb->write_locks); +lockdep_assert_held (&eb->lock); }#else static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) { }
fs/btrfs/print-tree.c +2 -9
@@ -151,15 +151,8 @@ static void print_uuid_item(struct extent_buffer *l, unsigned long offset,static void print_eb_refs_lock (struct extent_buffer *eb) {#ifdef CONFIG_BTRFS_DEBUG -btrfs_info (eb->fs_info, -"refs %u lock (w:%d r:%d bw:%d br:%d sw:%d sr:%d) lock_owner %u current %u" , -atomic_read (&eb->refs), eb->write_locks, -atomic_read (&eb->read_locks), - eb->blocking_writers, -atomic_read (&eb->blocking_readers), - eb->spinning_writers, -atomic_read (&eb->spinning_readers), - eb->lock_owner, current->pid); +btrfs_info (eb->fs_info,"refs %u lock_owner %u current %u" , +atomic_read (&eb->refs), eb->lock_owner, current->pid);#endif }
Signature · 1 line
2.24.1
PATCH 11/12 btrfs: remove all of the blocking helpers
In reply to Josef BacikNow that we're using a rw_semaphore we no longer need to indicate if a lock is blocking or not, nor do we need to flip the entire path from blocking to spinning. Remove these helpers and all the places they are called.
--- fs/btrfs/backref.c | 10 ++--- fs/btrfs/ctree.c | 91 ++++++---------------------------------- fs/btrfs/delayed-inode.c | 7 ---- fs/btrfs/disk-io.c | 8 +--- fs/btrfs/extent-tree.c | 19 +++------ fs/btrfs/file.c | 3 +- fs/btrfs/inode.c | 1 - fs/btrfs/locking.c | 74 -------------------------------- fs/btrfs/locking.h | 11 +---- fs/btrfs/qgroup.c | 9 ++-- fs/btrfs/ref-verify.c | 6 +-- fs/btrfs/relocation.c | 4 -- fs/btrfs/transaction.c | 2 - fs/btrfs/tree-defrag.c | 1 - fs/btrfs/tree-log.c | 3 -- 15 files changed, 30 insertions(+), 219 deletions(-)
fs/btrfs/backref.c +3 -7
@@ -1330,14 +1330,12 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans, goto out; } -if (!path->skip_locking) { +if (!path->skip_locking)btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - } ret =find_extent_in_eb (eb, bytenr, *extent_item_pos, &eie, ignore_offset);if (!path->skip_locking) -btrfs_tree_read_unlock_blocking (eb); +btrfs_tree_read_unlock (eb);free_extent_buffer (eb);if (ret <0 ) goto out;
@@ -1674,7 +1672,7 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, name_off, name_len);if (eb != eb_in) {if (!path->skip_locking) -btrfs_tree_read_unlock_blocking (eb); +btrfs_tree_read_unlock (eb);free_extent_buffer (eb); } ret = btrfs_find_item(fs_root, path, parent, 0,
@@ -1694,8 +1692,6 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, eb = path->nodes[0 ];/* make sure we can use eb after releasing the path */ if (eb != eb_in) { -if (!path->skip_locking) -btrfs_set_lock_blocking_read (eb); path->nodes[0 ] =NULL ; path->locks[0 ] =0 ; }
fs/btrfs/ctree.c +12 -79
325 lines not on this page · raw message
fs/btrfs/delayed-inode.c +0 -7
@@ -741,13 +741,6 @@ static int btrfs_batch_insert_items(struct btrfs_root *root, goto out; } -/* -* we need allocate some memory space, but it might cause the task -* to sleep, so we set all locked nodes in the path to blocking locks -* first. -*/ -btrfs_set_path_blocking (path); - keys =kmalloc_array (nitems,sizeof (struct btrfs_key ),GFP_NOFS );if (!keys) { ret = -ENOMEM ;
fs/btrfs/disk-io.c +2 -6
@@ -296,10 +296,8 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,if (atomic)return -EAGAIN ; -if (need_lock) { +if (need_lock)btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - }lock_extent_bits (io_tree, eb->start, eb->start + eb->len -1 , &cached_state);
@@ -328,7 +326,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,unlock_extent_cached (io_tree, eb->start, eb->start + eb->len -1 , &cached_state);if (need_lock) -btrfs_tree_read_unlock_blocking (eb); +btrfs_tree_read_unlock (eb);return ret; }
@@ -1071,8 +1069,6 @@ void btrfs_clean_tree_block(struct extent_buffer *buf)percpu_counter_add_batch (&fs_info->dirty_metadata_bytes, -buf->len, fs_info->dirty_metadata_batch); -/* ugh, clear_extent_buffer_dirty needs to lock the page */ -btrfs_set_lock_blocking_write (buf);clear_extent_buffer_dirty (buf); } }
fs/btrfs/extent-tree.c +6 -13
@@ -4528,7 +4528,6 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,btrfs_clean_tree_block (buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags); -btrfs_set_lock_blocking_write (buf);set_extent_buffer_uptodate (buf);memzero_extent_buffer (buf,0 ,sizeof (struct btrfs_header ));
@@ -4917,7 +4916,6 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, reada =1 ; }btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next); ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, &wc->refs[level - 1],
@@ -4977,7 +4975,6 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,return -EIO ; }btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next); } level--;
@@ -4989,7 +4986,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, } path->nodes[level] = next; path->slots[level] =0 ; - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; wc->level = level;if (wc->level ==1 ) wc->reada_slot =0 ;
@@ -5117,8 +5114,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (!path->locks[level]) {BUG_ON (level ==0 );btrfs_tree_lock (eb); -btrfs_set_lock_blocking_write (eb); - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; ret = btrfs_lookup_extent_info(trans, fs_info, eb->start, level, 1,
@@ -5161,8 +5157,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (!path->locks[level] &&btrfs_header_generation (eb) == trans->transid) {btrfs_tree_lock (eb); -btrfs_set_lock_blocking_write (eb); - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; }btrfs_clean_tree_block (eb); }
@@ -5330,9 +5325,8 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc)if (btrfs_disk_key_objectid (&root_item->drop_progress) ==0 ) { level =btrfs_header_level (root->node); path->nodes[level] =btrfs_lock_root_node (root); -btrfs_set_lock_blocking_write (path->nodes[level]); path->slots[level] =0 ; - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ;memset (&wc->update_progress,0 ,sizeof (wc->update_progress)); }else {
@@ -5360,8 +5354,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc) level =btrfs_header_level (root->node);while (1 ) {btrfs_tree_lock (path->nodes[level]); -btrfs_set_lock_blocking_write (path->nodes[level]); - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; ret = btrfs_lookup_extent_info(trans, fs_info, path->nodes[level]->start,
@@ -5548,7 +5541,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans, level =btrfs_header_level (node); path->nodes[level] = node; path->slots[level] =0 ; - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; wc->refs[parent_level] =1 ; wc->flags[parent_level] =BTRFS_BLOCK_FLAG_FULL_BACKREF ;
fs/btrfs/file.c +1 -2
@@ -1042,8 +1042,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,* write lock. */ if (!ret && replace_extent && leafs_visited ==1 && - (path->locks[0 ] ==BTRFS_WRITE_LOCK_BLOCKING || - path->locks[0 ] ==BTRFS_WRITE_LOCK ) && + path->locks[0 ] ==BTRFS_WRITE_LOCK &&btrfs_leaf_free_space (leaf) >=sizeof (struct btrfs_item ) + extent_item_size) {
fs/btrfs/inode.c +0 -1
@@ -6695,7 +6695,6 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, em->orig_start = em->start; ptr =btrfs_file_extent_inline_start (item) + extent_offset; -btrfs_set_path_blocking (path);if (!PageUptodate (page)) {if (btrfs_file_extent_compression (leaf, item) !=BTRFS_COMPRESS_NONE ) {
fs/btrfs/locking.c +0 -74
98 lines not on this page · raw message
fs/btrfs/locking.h +1 -10
@@ -13,8 +13,6 @@#define BTRFS_WRITE_LOCK 1#define BTRFS_READ_LOCK 2 -#define BTRFS_WRITE_LOCK_BLOCKING 3 -#define BTRFS_READ_LOCK_BLOCKING 4/* * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
@@ -93,12 +91,8 @@ void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting ne bool recurse);void btrfs_tree_read_lock (struct extent_buffer *eb);void btrfs_tree_read_unlock (struct extent_buffer *eb); -void btrfs_tree_read_unlock_blocking (struct extent_buffer *eb); -void btrfs_set_lock_blocking_read (struct extent_buffer *eb); -void btrfs_set_lock_blocking_write (struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);int btrfs_try_tree_write_lock (struct extent_buffer *eb); -int btrfs_tree_read_lock_atomic (struct extent_buffer *eb);struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root);struct extent_buffer *__btrfs_read_lock_root_node (struct btrfs_root *root,bool recurse);
@@ -117,15 +111,12 @@ static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) {static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) { }#endif -void btrfs_set_path_blocking (struct btrfs_path *p);void btrfs_unlock_up_safe (struct btrfs_path *path,int level);static inline void btrfs_tree_unlock_rw (struct extent_buffer *eb,int rw) { -if (rw ==BTRFS_WRITE_LOCK || rw ==BTRFS_WRITE_LOCK_BLOCKING ) +if (rw ==BTRFS_WRITE_LOCK )btrfs_tree_unlock (eb); -else if (rw ==BTRFS_READ_LOCK_BLOCKING ) -btrfs_tree_read_unlock_blocking (eb);else if (rw ==BTRFS_READ_LOCK )btrfs_tree_read_unlock (eb);else
fs/btrfs/qgroup.c +3 -6
@@ -1902,8 +1902,7 @@ static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans, src_path->nodes[cur_level] = eb;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - src_path->locks[cur_level] =BTRFS_READ_LOCK_BLOCKING ; + src_path->locks[cur_level] =BTRFS_READ_LOCK ; } src_path->slots[cur_level] = dst_path->slots[cur_level];
@@ -2043,8 +2042,7 @@ static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans, dst_path->slots[cur_level] =0 ;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - dst_path->locks[cur_level] =BTRFS_READ_LOCK_BLOCKING ; + dst_path->locks[cur_level] =BTRFS_READ_LOCK ; need_cleanup = true; }
@@ -2218,8 +2216,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans, path->slots[level] =0 ;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - path->locks[level] =BTRFS_READ_LOCK_BLOCKING ; + path->locks[level] =BTRFS_READ_LOCK ; ret = btrfs_qgroup_trace_extent(trans, child_bytenr, fs_info->nodesize,
fs/btrfs/ref-verify.c +2 -4
@@ -575,10 +575,9 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,return -EIO ; }btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); path->nodes[level-1 ] = eb; path->slots[level-1 ] =0 ; - path->locks[level-1 ] =BTRFS_READ_LOCK_BLOCKING ; + path->locks[level-1 ] =BTRFS_READ_LOCK ; }else { ret =process_leaf (root, path, bytenr, num_bytes);if (ret)
@@ -1000,11 +999,10 @@ int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)return -ENOMEM ; eb =btrfs_read_lock_root_node (fs_info->extent_root); -btrfs_set_lock_blocking_read (eb); level =btrfs_header_level (eb); path->nodes[level] = eb; path->slots[level] =0 ; - path->locks[level] =BTRFS_READ_LOCK_BLOCKING ; + path->locks[level] =BTRFS_READ_LOCK ;while (1 ) {/*
fs/btrfs/relocation.c +0 -4
@@ -1196,7 +1196,6 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,btrfs_node_key_to_cpu (path->nodes[lowest_level], &key, slot); eb =btrfs_lock_root_node (dest); -btrfs_set_lock_blocking_write (eb); level =btrfs_header_level (eb);if (level < lowest_level) {
@@ -1210,7 +1209,6 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, BTRFS_NESTING_COW);BUG_ON (ret); } -btrfs_set_lock_blocking_write (eb);if (next_key) { next_key->objectid = (u64)-1 ;
@@ -1279,7 +1277,6 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, BTRFS_NESTING_COW);BUG_ON (ret); } -btrfs_set_lock_blocking_write (eb);btrfs_tree_unlock (parent);free_extent_buffer (parent);
@@ -2307,7 +2304,6 @@ static int do_relocation(struct btrfs_trans_handle *trans, goto next; }btrfs_tree_lock (eb); -btrfs_set_lock_blocking_write (eb);if (!node->eb) { ret = btrfs_cow_block(trans, root, eb, upper->eb,
fs/btrfs/transaction.c +0 -2
@@ -1596,8 +1596,6 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, goto fail; } -btrfs_set_lock_blocking_write (old); - ret =btrfs_copy_root (trans, root, old, &tmp, objectid);/* clean up in any case */ btrfs_tree_unlock (old);
fs/btrfs/tree-defrag.c +0 -1
@@ -52,7 +52,6 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,u32 nritems; root_node =btrfs_lock_root_node (root); -btrfs_set_lock_blocking_write (root_node); nritems =btrfs_header_nritems (root_node); root->defrag_max.objectid =0 ;/* from above we know this is not a leaf */
fs/btrfs/tree-log.c +0 -3
@@ -2736,7 +2736,6 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next);btrfs_clean_tree_block (next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
@@ -2805,7 +2804,6 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next);btrfs_clean_tree_block (next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
@@ -2887,7 +2885,6 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next);btrfs_clean_tree_block (next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
Signature · 1 line
2.24.1
PATCH 12/12 btrfs: rip out path->leave_spinning
In reply to Josef BacikWe no longer distinguish between blocking and spinning, so rip out all of this code.
--- fs/btrfs/backref.c | 3 --- fs/btrfs/ctree.c | 3 --- fs/btrfs/ctree.h | 1 - fs/btrfs/delayed-inode.c | 4 ---- fs/btrfs/dir-item.c | 1 - fs/btrfs/export.c | 1 - fs/btrfs/extent-tree.c | 8 -------- fs/btrfs/extent_io.c | 1 - fs/btrfs/file-item.c | 4 ---- fs/btrfs/free-space-tree.c | 2 -- fs/btrfs/inode-item.c | 6 ------ fs/btrfs/inode.c | 12 ------------ fs/btrfs/ioctl.c | 1 - fs/btrfs/qgroup.c | 2 -- fs/btrfs/reflink.c | 3 --- fs/btrfs/super.c | 2 -- fs/btrfs/tests/qgroup-tests.c | 4 ---- 17 files changed, 58 deletions(-)
fs/btrfs/backref.c +0 -3
@@ -1658,13 +1658,11 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,s64 bytes_left = ((s64 )size) -1 ;struct extent_buffer *eb = eb_in;struct btrfs_key found_key; -int leave_spinning = path->leave_spinning;struct btrfs_inode_ref *iref;if (bytes_left >=0 ) dest[bytes_left] ='\0' ; - path->leave_spinning =1 ;while (1 ) { bytes_left -= name_len;if (bytes_left >=0 )
@@ -1708,7 +1706,6 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, }btrfs_release_path (path); - path->leave_spinning = leave_spinning;if (ret)return ERR_PTR (ret);
fs/btrfs/ctree.c +0 -3
@@ -5237,7 +5237,6 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,struct btrfs_key key;u32 nritems;int ret; -int old_spinning = path->leave_spinning;int next_rw_lock =0 ; nritems =btrfs_header_nritems (path->nodes[0 ]);
@@ -5252,7 +5251,6 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,btrfs_release_path (path); path->keep_locks =1 ; - path->leave_spinning =1 ;if (time_seq) ret =btrfs_search_old_slot (root, &key, path, time_seq);
@@ -5387,7 +5385,6 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, ret =0 ; done:unlock_up (path,0 ,1 ,0 ,NULL ); - path->leave_spinning = old_spinning;return ret; }
fs/btrfs/ctree.h +0 -1
@@ -370,7 +370,6 @@ struct btrfs_path { unsigned int search_for_split:1; unsigned int keep_locks:1; unsigned int skip_locking:1; - unsigned int leave_spinning:1; unsigned int search_commit_root:1; unsigned int need_commit_sem:1; unsigned int skip_release_on_error:1;
fs/btrfs/delayed-inode.c +0 -4
@@ -1149,7 +1149,6 @@ static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr) path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ; block_rsv = trans->block_rsv; trans->block_rsv = &fs_info->delayed_block_rsv;
@@ -1214,7 +1213,6 @@ int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,btrfs_release_delayed_node (delayed_node);return -ENOMEM ; } - path->leave_spinning =1 ; block_rsv = trans->block_rsv; trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
@@ -1259,7 +1257,6 @@ int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode) ret = -ENOMEM ; goto trans_out; } - path->leave_spinning =1 ; block_rsv = trans->block_rsv; trans->block_rsv = &fs_info->delayed_block_rsv;
@@ -1328,7 +1325,6 @@ static void btrfs_async_run_delayed_root(struct btrfs_work *work)if (!delayed_node)break ; - path->leave_spinning =1 ; root = delayed_node->root; trans =btrfs_join_transaction (root);
fs/btrfs/dir-item.c +0 -1
@@ -127,7 +127,6 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name, path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ;btrfs_cpu_key_to_disk (&disk_key, location);
fs/btrfs/export.c +0 -1
@@ -222,7 +222,6 @@ static int btrfs_get_name(struct dentry *parent, char *name, path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ;if (ino ==BTRFS_FIRST_FREE_OBJECTID ) { key.objectid =BTRFS_I (inode)->root->root_key.objectid;
fs/btrfs/extent-tree.c +0 -8
@@ -1448,7 +1448,6 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ;/* this will setup the path even if it fails to insert the back ref */ ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, parent, root_objectid, owner,
@@ -1472,7 +1471,6 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,btrfs_mark_buffer_dirty (leaf);btrfs_release_path (path); - path->leave_spinning =1 ;/* now insert the actual backref */ if (owner <BTRFS_FIRST_FREE_OBJECTID ) {BUG_ON (refs_to_add !=1 );
@@ -1588,7 +1586,6 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans, } again: - path->leave_spinning =1 ; ret =btrfs_search_slot (trans, fs_info->extent_root, &key, path,0 ,1 );if (ret <0 ) { err = ret;
@@ -2959,8 +2956,6 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - is_data = owner_objectid >=BTRFS_FIRST_FREE_OBJECTID ;BUG_ON (!is_data && refs_to_drop !=1 );
@@ -3002,7 +2997,6 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, goto out; }btrfs_release_path (path); - path->leave_spinning =1 ; key.objectid = bytenr; key.type =BTRFS_EXTENT_ITEM_KEY ;
@@ -4292,7 +4286,6 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, fs_info->extent_root, path, ins, size);if (ret) {
@@ -4377,7 +4370,6 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, fs_info->extent_root, path, &extent_key, size);if (ret) {
fs/btrfs/extent_io.c +0 -1
@@ -4727,7 +4727,6 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ; roots =ulist_alloc (GFP_KERNEL ); tmp_ulist =ulist_alloc (GFP_KERNEL );
fs/btrfs/file-item.c +0 -4
@@ -142,7 +142,6 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, file_key.offset = pos; file_key.type =BTRFS_EXTENT_DATA_KEY ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &file_key,sizeof (*item));if (ret <0 )
@@ -706,7 +705,6 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, key.offset = end_byte -1 ; key.type =BTRFS_EXTENT_CSUM_KEY ; - path->leave_spinning =1 ; ret =btrfs_search_slot (trans, root, &key, path,-1 ,1 );if (ret >0 ) {if (path->slots[0 ] ==0 )
@@ -991,10 +989,8 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, }else { ins_size = csum_size; } - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &file_key, ins_size); - path->leave_spinning =0 ;if (ret <0 ) goto out;if (WARN_ON (ret !=0 ))
fs/btrfs/free-space-tree.c +0 -2
@@ -1191,8 +1191,6 @@ static int clear_free_space_tree(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - key.objectid =0 ; key.type =0 ; key.offset =0 ;
fs/btrfs/inode-item.c +0 -6
@@ -119,8 +119,6 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - ret =btrfs_search_slot (trans, root, &key, path,-1 ,1 );if (ret >0 ) ret = -ENOENT ;
@@ -193,8 +191,6 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - ret =btrfs_search_slot (trans, root, &key, path,-1 ,1 );if (ret >0 ) { ret = -ENOENT ;
@@ -270,7 +266,6 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &key, ins_len);if (ret == -EEXIST ) {
@@ -327,7 +322,6 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; path->skip_release_on_error =1 ; ret =btrfs_insert_empty_item (trans, root, path, &key, ins_len);
fs/btrfs/inode.c +0 -12
@@ -197,7 +197,6 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, key.type =BTRFS_EXTENT_DATA_KEY ; datasize =btrfs_file_extent_calc_inline_size (cur_size); - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &key, datasize);if (ret)
@@ -2499,7 +2498,6 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, ins.offset = file_pos; ins.type =BTRFS_EXTENT_DATA_KEY ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &ins,sizeof (*stack_fi));if (ret)
@@ -3481,7 +3479,6 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_lookup_inode (trans, root, path, &BTRFS_I (inode)->location,1 );if (ret) {
@@ -3570,7 +3567,6 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, goto out; } - path->leave_spinning =1 ; di =btrfs_lookup_dir_item (trans, root, path, dir_ino, name, name_len,-1 );if (IS_ERR_OR_NULL (di)) {
@@ -6026,7 +6022,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, goto fail; } - path->leave_spinning =1 ; ret =btrfs_insert_empty_items (trans, root, path, key, sizes, nitems);if (ret !=0 ) goto fail_unlock;
@@ -6585,13 +6580,6 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,/* Chances are we'll be called again, so go ahead and do readahead */ path->reada =READA_FORWARD ; - -/* -* Unless we're going to uncompress the inline extent, no sleep would -* happen. -*/ - path->leave_spinning =1 ; - path->recurse =btrfs_is_free_space_inode (inode); ret =btrfs_lookup_file_extent (NULL , root, path, objectid, start,0 );
fs/btrfs/ioctl.c +0 -1
@@ -3382,7 +3382,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) ret = -ENOMEM ; goto out_free; } - path->leave_spinning =1 ; trans =btrfs_start_transaction (root,1 );if (IS_ERR (trans)) {
fs/btrfs/qgroup.c +0 -2
@@ -893,8 +893,6 @@ static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - key.objectid =0 ; key.offset =0 ; key.type =0 ;
fs/btrfs/reflink.c +0 -3
@@ -347,7 +347,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,u64 drop_start;/* Note the key will change type as we walk through the tree */ - path->leave_spinning =1 ; ret =btrfs_search_slot (NULL ,BTRFS_I (src)->root, &key, path,0 ,0 );if (ret <0 )
@@ -417,7 +416,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode, size);btrfs_release_path (path); - path->leave_spinning =0 ;memcpy (&new_key, &key,sizeof (new_key)); new_key.objectid =btrfs_ino (BTRFS_I (inode));
@@ -531,7 +529,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,* mixing buffered and direct IO writes against this file. */ btrfs_release_path (path); - path->leave_spinning =0 ; ret =btrfs_punch_hole_range (inode, path, last_dest_end, destoff + len -1 ,NULL , &trans);
fs/btrfs/super.c +0 -2
@@ -1127,7 +1127,6 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, ret = -ENOMEM ; goto err; } - path->leave_spinning =1 ; name =kmalloc (PATH_MAX ,GFP_KERNEL );if (!name) {
@@ -1256,7 +1255,6 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ;/* * Find the "default" dir item which points to the root item that we
fs/btrfs/tests/qgroup-tests.c +0 -4
@@ -36,7 +36,6 @@ static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (&trans, root, path, &ins, size);if (ret) {test_err ("couldn't insert ref %d" , ret);
@@ -86,7 +85,6 @@ static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_search_slot (&trans, root, &key, path,0 ,1 );if (ret) {test_err ("couldn't find extent ref" );
@@ -135,7 +133,6 @@ static int remove_extent_item(struct btrfs_root *root, u64 bytenr,test_std_err (TEST_ALLOC_ROOT );return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_search_slot (&trans, root, &key, path,-1 ,1 );if (ret) {
@@ -170,7 +167,6 @@ static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_search_slot (&trans, root, &key, path,0 ,1 );if (ret) {test_err ("couldn't find extent ref" );
Signature · 1 line
2.24.1
Quoted 3 lines · Josef Bacik
v1->v2:
- broke the lockdep fixes out into their own series.
- added a new fix where we need to handle double splitting leaves in some cases.
The split lockdep patches are now in misc-next, I'd like to continue with a piecemal steps with the whole rwsem series:
- preparatory stuff, like the annotations patches 1-10
- switch locks to rwsem (1 patch)
- post cleanups once things prove to be ok
So I'll take 1-10 now, this should not significantly affect things. We have the data tickets in and I'd rather wait a bit before adding another similarly intrusive change so we'd be able to identify where bugs originate.
Quoted 9 lines · David Sterba
Quoted 3 lines · Josef Bacik
v1->v2:
- broke the lockdep fixes out into their own series.
- added a new fix where we need to handle double splitting leaves in some cases.The split lockdep patches are now in misc-next, I'd like to continue with a piecemal steps with the whole rwsem series:
- preparatory stuff, like the annotations patches 1-10
1-9 are now in misc-next.
Quoted 1 line
- switch locks to rwsem (1 patch)
I'll add that to for-next. This is a significant change, so far the other big changes seem to be witout problems (data tickets, iomap-dio). The plan is to keep the rwsem, with fallback to reverting this patch in case something bad pops up. The lockdep warnings triggered by rwsem should be fixed in misc-next.
Quoted 11 lines · David Sterba
Quoted 3 lines · Josef Bacik
v1->v2:
- broke the lockdep fixes out into their own series.
- added a new fix where we need to handle double splitting leaves in some cases.The split lockdep patches are now in misc-next, I'd like to continue with a piecemal steps with the whole rwsem series:
- preparatory stuff, like the annotations patches 1-10
- switch locks to rwsem (1 patch)
- post cleanups once things prove to be ok
The rwsem seems to be working ok, the two remaining patches have been added to misc-next. RIP tree locks.