Extent buffer locking cleanups, part 1
David Sterba
maintainercover Extent buffer locking cleanups, part 1
This series cleans up a few locking helpers, removes conditional switches by read/write type where not necessary and updates the callers. No changes expected, other than tiny speedups resulting from smaller code.
David Sterba (6):
btrfs: split btrfs_set_lock_blocking_rw to read and write helpers
btrfs: split btrfs_clear_lock_blocking_rw to read and write helpers
btrfs: replace btrfs_set_lock_blocking_rw with appropriate helpers
btrfs: open code now trivial btrfs_set_lock_blocking
btrfs: simplify waiting loop in btrfs_tree_lock
btrfs: merge btrfs_set_lock_blocking_rw with it's caller
fs/btrfs/backref.c | 4 +- fs/btrfs/ctree.c | 39 +++++++++------ fs/btrfs/disk-io.c | 4 +- fs/btrfs/extent-tree.c | 14 +++--- fs/btrfs/locking.c | 108 ++++++++++++++++++++--------------------- fs/btrfs/locking.h | 15 ++---- fs/btrfs/qgroup.c | 6 +-- fs/btrfs/ref-verify.c | 4 +- fs/btrfs/relocation.c | 8 +-- fs/btrfs/transaction.c | 2 +- fs/btrfs/tree-defrag.c | 2 +- fs/btrfs/tree-log.c | 6 +-- 12 files changed, 105 insertions(+), 107 deletions(-)
Signature · 1 line
2.20.1
David Sterba
maintainerPATCH 1/6 btrfs: split btrfs_set_lock_blocking_rw to read and write helpers
There are many callers that hardcode the desired lock type so we can avoid the switch and call them directly. Split the current function to two but leave a helper that still takes the variable lock type to make current code compile. The call sites will be converted in followup patches.
--- fs/btrfs/locking.c | 50 ++++++++++++++++++++++++---------------------- fs/btrfs/locking.h | 15 +++++++++++++- 2 files changed, 40 insertions(+), 25 deletions(-)
fs/btrfs/locking.c +26 -24
@@ -14,35 +14,37 @@static void btrfs_assert_tree_read_locked (struct extent_buffer *eb); -/* -* if we currently have a spinning reader or writer lock -* (indicated by the rw flag) this will bump the count -* of blocking holders and drop the spinlock. -*/ -void btrfs_set_lock_blocking_rw (struct extent_buffer *eb,int rw) +void btrfs_set_lock_blocking_read (struct extent_buffer *eb) {/* -* no lock is required. The lock owner may change if -* we have a read 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. +* No lock is required. The lock owner may change if we have a read +* 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)return ; -if (rw ==BTRFS_WRITE_LOCK ) { -if (atomic_read (&eb->blocking_writers) ==0 ) { -WARN_ON (atomic_read (&eb->spinning_writers) !=1 ); -atomic_dec (&eb->spinning_writers); -btrfs_assert_tree_locked (eb); -atomic_inc (&eb->blocking_writers); -write_unlock (&eb->lock); - } - }else if (rw ==BTRFS_READ_LOCK ) { -btrfs_assert_tree_read_locked (eb); -atomic_inc (&eb->blocking_readers); -WARN_ON (atomic_read (&eb->spinning_readers) ==0 ); -atomic_dec (&eb->spinning_readers); -read_unlock (&eb->lock); +btrfs_assert_tree_read_locked (eb); +atomic_inc (&eb->blocking_readers); +WARN_ON (atomic_read (&eb->spinning_readers) ==0 ); +atomic_dec (&eb->spinning_readers); +read_unlock (&eb->lock); +} + +void btrfs_set_lock_blocking_write (struct extent_buffer *eb) +{ +/* +* No lock is required. The lock owner may change if we have a read +* 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) +return ; +if (atomic_read (&eb->blocking_writers) ==0 ) { +WARN_ON (atomic_read (&eb->spinning_writers) !=1 ); +atomic_dec (&eb->spinning_writers); +btrfs_assert_tree_locked (eb); +atomic_inc (&eb->blocking_writers); +write_unlock (&eb->lock); } }
fs/btrfs/locking.h +14 -1
@@ -17,7 +17,8 @@ void btrfs_tree_unlock(struct extent_buffer *eb);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_rw (struct extent_buffer *eb,int rw); +void btrfs_set_lock_blocking_read (struct extent_buffer *eb); +void btrfs_set_lock_blocking_write (struct extent_buffer *eb);void btrfs_clear_lock_blocking_rw (struct extent_buffer *eb,int rw);void btrfs_assert_tree_locked (struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);
@@ -37,6 +38,18 @@ static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)BUG (); } +/* +* If we currently have a spinning reader or writer lock (indicated by the rw +* flag) this will bump the count of blocking holders and drop the spinlock. +*/ +static inline void btrfs_set_lock_blocking_rw (struct extent_buffer *eb,int rw) +{ +if (rw ==BTRFS_WRITE_LOCK ) +btrfs_set_lock_blocking_write (eb); +else if (rw ==BTRFS_READ_LOCK ) +btrfs_set_lock_blocking_read (eb); +} +static inline void btrfs_set_lock_blocking (struct extent_buffer *eb) {btrfs_set_lock_blocking_rw (eb,BTRFS_WRITE_LOCK );
Signature · 1 line
2.20.1
Re: [PATCH 1/6] btrfs: split btrfs_set_lock_blocking_rw to read and write helpers
Looks good,
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
David Sterba
maintainerPATCH 2/6 btrfs: split btrfs_clear_lock_blocking_rw to read and write helpers
In reply to David SterbaThere are many callers that hardcode the desired lock type so we can avoid the switch and call them directly. Split the current function to two. There are no remaining users of btrfs_clear_lock_blocking_rw so it's removed. The call sites will be converted in followup patches.
--- fs/btrfs/locking.c | 47 ++++++++++++++++++++++++---------------------- fs/btrfs/locking.h | 7 ++----- 2 files changed, 27 insertions(+), 27 deletions(-)
fs/btrfs/locking.c +25 -22
@@ -48,11 +48,24 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb) } } -/* -* if we currently have a blocking lock, take the spinlock -* and drop our blocking count -*/ -void btrfs_clear_lock_blocking_rw (struct extent_buffer *eb,int rw) +void btrfs_clear_lock_blocking_read (struct extent_buffer *eb) +{ +/* +* No lock is required. The lock owner may change if we have a read +* 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) +return ; +BUG_ON (atomic_read (&eb->blocking_readers) ==0 ); +read_lock (&eb->lock); +atomic_inc (&eb->spinning_readers); +/* atomic_dec_and_test implies a barrier */ +if (atomic_dec_and_test (&eb->blocking_readers)) +cond_wake_up_nomb (&eb->read_lock_wq); +} + +void btrfs_clear_lock_blocking_write (struct extent_buffer *eb) {/* * no lock is required. The lock owner may change if
@@ -62,23 +75,13 @@ void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)*/ if (eb->lock_nested && current->pid == eb->lock_owner)return ; - -if (rw ==BTRFS_WRITE_LOCK_BLOCKING ) { -BUG_ON (atomic_read (&eb->blocking_writers) !=1 ); -write_lock (&eb->lock); -WARN_ON (atomic_read (&eb->spinning_writers)); -atomic_inc (&eb->spinning_writers); -/* atomic_dec_and_test implies a barrier */ -if (atomic_dec_and_test (&eb->blocking_writers)) -cond_wake_up_nomb (&eb->write_lock_wq); - }else if (rw ==BTRFS_READ_LOCK_BLOCKING ) { -BUG_ON (atomic_read (&eb->blocking_readers) ==0 ); -read_lock (&eb->lock); -atomic_inc (&eb->spinning_readers); -/* atomic_dec_and_test implies a barrier */ -if (atomic_dec_and_test (&eb->blocking_readers)) -cond_wake_up_nomb (&eb->read_lock_wq); - } +BUG_ON (atomic_read (&eb->blocking_writers) !=1 ); +write_lock (&eb->lock); +WARN_ON (atomic_read (&eb->spinning_writers)); +atomic_inc (&eb->spinning_writers); +/* atomic_dec_and_test implies a barrier */ +if (atomic_dec_and_test (&eb->blocking_writers)) +cond_wake_up_nomb (&eb->write_lock_wq); }/*
fs/btrfs/locking.h +2 -5
@@ -19,7 +19,8 @@ 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); -void btrfs_clear_lock_blocking_rw (struct extent_buffer *eb,int rw); +void btrfs_clear_lock_blocking_read (struct extent_buffer *eb); +void btrfs_clear_lock_blocking_write (struct extent_buffer *eb);void btrfs_assert_tree_locked (struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);int btrfs_try_tree_write_lock (struct extent_buffer *eb);
@@ -55,8 +56,4 @@ static inline void btrfs_set_lock_blocking(struct extent_buffer *eb) btrfs_set_lock_blocking_rw(eb, BTRFS_WRITE_LOCK); } -static inline void btrfs_clear_lock_blocking (struct extent_buffer *eb) -{ -btrfs_clear_lock_blocking_rw (eb,BTRFS_WRITE_LOCK_BLOCKING ); -}#endif
Signature · 1 line
2.20.1
Re: [PATCH 2/6] btrfs: split btrfs_clear_lock_blocking_rw to read and write helpers
Looks good,
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
David Sterba
maintainerPATCH 3/6 btrfs: replace btrfs_set_lock_blocking_rw with appropriate helpers
In reply to David SterbaWe can use the right helper where the lock type is a fixed parameter.
--- fs/btrfs/backref.c | 4 ++-- fs/btrfs/ctree.c | 4 ++-- fs/btrfs/disk-io.c | 2 +- fs/btrfs/locking.h | 2 +- fs/btrfs/qgroup.c | 6 +++--- fs/btrfs/ref-verify.c | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-)
fs/btrfs/backref.c +2 -2
@@ -1289,7 +1289,7 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans, goto out; }btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); ret =find_extent_in_eb (eb, bytenr, *extent_item_pos, &eie, ignore_offset);btrfs_tree_read_unlock_blocking (eb);
@@ -1650,7 +1650,7 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,/* make sure we can use eb after releasing the path */ if (eb != eb_in) {if (!path->skip_locking) -btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); path->nodes[0 ] =NULL ; path->locks[0 ] =0 ; }
fs/btrfs/ctree.c +2 -2
@@ -1266,7 +1266,7 @@ tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,return eb;btrfs_set_path_blocking (path); -btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb);if (tm->op ==MOD_LOG_KEY_REMOVE_WHILE_FREEING ) {BUG_ON (tm->slot !=0 );
@@ -1356,7 +1356,7 @@ get_old_root(struct btrfs_root *root, u64 time_seq)free_extent_buffer (eb_root); eb =alloc_dummy_extent_buffer (fs_info, logical); }else { -btrfs_set_lock_blocking_rw (eb_root,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb_root); eb =btrfs_clone_extent_buffer (eb_root);btrfs_tree_read_unlock_blocking (eb_root);free_extent_buffer (eb_root);
fs/btrfs/disk-io.c +1 -1
@@ -341,7 +341,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,if (need_lock) {btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); } lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
fs/btrfs/locking.h +1 -1
@@ -53,7 +53,7 @@ static inline void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw)static inline void btrfs_set_lock_blocking (struct extent_buffer *eb) { -btrfs_set_lock_blocking_rw (eb,BTRFS_WRITE_LOCK ); +btrfs_set_lock_blocking_write (eb); }#endif
fs/btrfs/qgroup.c +3 -3
@@ -1832,7 +1832,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_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); src_path->locks[cur_level] =BTRFS_READ_LOCK_BLOCKING ; }
@@ -1973,7 +1973,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_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); dst_path->locks[cur_level] =BTRFS_READ_LOCK_BLOCKING ; need_cleanup = true; }
@@ -2207,7 +2207,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans, path->slots[level] =0 ;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); path->locks[level] =BTRFS_READ_LOCK_BLOCKING ; ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
fs/btrfs/ref-verify.c +2 -2
@@ -583,7 +583,7 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,return -EIO ; }btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); path->nodes[level-1 ] = eb; path->slots[level-1 ] =0 ; path->locks[level-1 ] =BTRFS_READ_LOCK_BLOCKING ;
@@ -987,7 +987,7 @@ 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_rw (eb,BTRFS_READ_LOCK ); +btrfs_set_lock_blocking_read (eb); level =btrfs_header_level (eb); path->nodes[level] = eb; path->slots[level] =0 ;
Signature · 1 line
2.20.1
Re: [PATCH 3/6] btrfs: replace btrfs_set_lock_blocking_rw with appropriate helpers
Looks good,
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
David Sterba
maintainerPATCH 4/6 btrfs: open code now trivial btrfs_set_lock_blocking
In reply to David Sterbabtrfs_set_lock_blocking is now only a simple wrapper around btrfs_set_lock_blocking_write. The name does not bring any semantic value that could not be inferred from the new function so there's no point keeping it.
--- fs/btrfs/ctree.c | 22 +++++++++++----------- fs/btrfs/disk-io.c | 2 +- fs/btrfs/extent-tree.c | 14 +++++++------- fs/btrfs/locking.h | 5 ----- fs/btrfs/relocation.c | 8 ++++---- fs/btrfs/transaction.c | 2 +- fs/btrfs/tree-defrag.c | 2 +- fs/btrfs/tree-log.c | 6 +++--- 8 files changed, 28 insertions(+), 33 deletions(-)
fs/btrfs/ctree.c +11 -11
@@ -1464,8 +1464,8 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, search_start = buf->start & ~((u64 )SZ_1G -1 );if (parent) -btrfs_set_lock_blocking (parent); -btrfs_set_lock_blocking (buf); +btrfs_set_lock_blocking_write (parent); +btrfs_set_lock_blocking_write (buf); ret =__btrfs_cow_block (trans, root, buf, parent, parent_slot, cow_ret, search_start,0 );
@@ -1560,7 +1560,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,if (parent_nritems <=1 )return 0 ; -btrfs_set_lock_blocking (parent); +btrfs_set_lock_blocking_write (parent);for (i = start_slot; i <= end_slot; i++) {struct btrfs_key first_key;
@@ -1619,7 +1619,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, search_start = last_block;btrfs_tree_lock (cur); -btrfs_set_lock_blocking (cur); +btrfs_set_lock_blocking_write (cur); err = __btrfs_cow_block(trans, root, cur, parent, i, &cur, search_start, min(16 * blocksize,
@@ -1834,7 +1834,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, }btrfs_tree_lock (child); -btrfs_set_lock_blocking (child); +btrfs_set_lock_blocking_write (child); ret =btrfs_cow_block (trans, root, child, mid,0 , &child);if (ret) {btrfs_tree_unlock (child);
@@ -1872,7 +1872,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,if (left) {btrfs_tree_lock (left); -btrfs_set_lock_blocking (left); +btrfs_set_lock_blocking_write (left); wret =btrfs_cow_block (trans, root, left, parent, pslot -1 , &left);if (wret) {
@@ -1887,7 +1887,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,if (right) {btrfs_tree_lock (right); -btrfs_set_lock_blocking (right); +btrfs_set_lock_blocking_write (right); wret =btrfs_cow_block (trans, root, right, parent, pslot +1 , &right);if (wret) {
@@ -2050,7 +2050,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,u32 left_nr;btrfs_tree_lock (left); -btrfs_set_lock_blocking (left); +btrfs_set_lock_blocking_write (left); left_nr =btrfs_header_nritems (left);if (left_nr >=BTRFS_NODEPTRS_PER_BLOCK (fs_info) -1 ) {
@@ -2105,7 +2105,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,u32 right_nr;btrfs_tree_lock (right); -btrfs_set_lock_blocking (right); +btrfs_set_lock_blocking_write (right); right_nr =btrfs_header_nritems (right);if (right_nr >=BTRFS_NODEPTRS_PER_BLOCK (fs_info) -1 ) {
@@ -3749,7 +3749,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_rootreturn 1 ;btrfs_tree_lock (right); -btrfs_set_lock_blocking (right); +btrfs_set_lock_blocking_write (right); free_space =btrfs_leaf_free_space (fs_info, right);if (free_space < data_size)
@@ -3983,7 +3983,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_rootreturn 1 ;btrfs_tree_lock (left); -btrfs_set_lock_blocking (left); +btrfs_set_lock_blocking_write (left); free_space =btrfs_leaf_free_space (fs_info, left);if (free_space < data_size) {
fs/btrfs/disk-io.c +1 -1
@@ -1120,7 +1120,7 @@ void clean_tree_block(struct btrfs_fs_info *fs_info, -buf->len, fs_info->dirty_metadata_batch);/* ugh, clear_extent_buffer_dirty needs to lock the page */ -btrfs_set_lock_blocking (buf); +btrfs_set_lock_blocking_write (buf);clear_extent_buffer_dirty (buf); } }
fs/btrfs/extent-tree.c +7 -7
@@ -8492,7 +8492,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,clean_tree_block (fs_info, buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags); -btrfs_set_lock_blocking (buf); +btrfs_set_lock_blocking_write (buf);set_extent_buffer_uptodate (buf);memzero_extent_buffer (buf,0 ,sizeof (struct btrfs_header ));
@@ -8917,7 +8917,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, reada =1 ; }btrfs_tree_lock (next); -btrfs_set_lock_blocking (next); +btrfs_set_lock_blocking_write (next); ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, &wc->refs[level - 1],
@@ -8977,7 +8977,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,return -EIO ; }btrfs_tree_lock (next); -btrfs_set_lock_blocking (next); +btrfs_set_lock_blocking_write (next); } level--;
@@ -9089,7 +9089,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 (eb); +btrfs_set_lock_blocking_write (eb); path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; ret = btrfs_lookup_extent_info(trans, fs_info,
@@ -9131,7 +9131,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 (eb); +btrfs_set_lock_blocking_write (eb); path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; }clean_tree_block (fs_info, eb);
@@ -9298,7 +9298,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,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 (path->nodes[level]); +btrfs_set_lock_blocking_write (path->nodes[level]); path->slots[level] =0 ; path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; memset(&wc->update_progress, 0,
@@ -9328,7 +9328,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, level =btrfs_header_level (root->node);while (1 ) {btrfs_tree_lock (path->nodes[level]); -btrfs_set_lock_blocking (path->nodes[level]); +btrfs_set_lock_blocking_write (path->nodes[level]); path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; ret = btrfs_lookup_extent_info(trans, fs_info,
fs/btrfs/locking.h +0 -5
@@ -51,9 +51,4 @@ static inline void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw) btrfs_set_lock_blocking_read(eb); } -static inline void btrfs_set_lock_blocking (struct extent_buffer *eb) -{ -btrfs_set_lock_blocking_write (eb); -} -#endif
fs/btrfs/relocation.c +4 -4
@@ -1773,7 +1773,7 @@ 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 (eb); +btrfs_set_lock_blocking_write (eb); level =btrfs_header_level (eb);if (level < lowest_level) {
@@ -1786,7 +1786,7 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, ret =btrfs_cow_block (trans, dest, eb,NULL ,0 , &eb);BUG_ON (ret); } -btrfs_set_lock_blocking (eb); +btrfs_set_lock_blocking_write (eb);if (next_key) { next_key->objectid = (u64)-1 ;
@@ -1852,7 +1852,7 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, slot, &eb);BUG_ON (ret); } -btrfs_set_lock_blocking (eb); +btrfs_set_lock_blocking_write (eb);btrfs_tree_unlock (parent);free_extent_buffer (parent);
@@ -2752,7 +2752,7 @@ static int do_relocation(struct btrfs_trans_handle *trans, goto next; }btrfs_tree_lock (eb); -btrfs_set_lock_blocking (eb); +btrfs_set_lock_blocking_write (eb);if (!node->eb) { ret = btrfs_cow_block(trans, root, eb, upper->eb,
fs/btrfs/transaction.c +1 -1
@@ -1540,7 +1540,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, goto fail; } -btrfs_set_lock_blocking (old); +btrfs_set_lock_blocking_write (old); ret =btrfs_copy_root (trans, root, old, &tmp, objectid);/* clean up in any case */
fs/btrfs/tree-defrag.c +1 -1
@@ -52,7 +52,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,u32 nritems; root_node =btrfs_lock_root_node (root); -btrfs_set_lock_blocking (root_node); +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 +3 -3
@@ -2663,7 +2663,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking (next); +btrfs_set_lock_blocking_write (next);clean_tree_block (fs_info, next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
@@ -2747,7 +2747,7 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking (next); +btrfs_set_lock_blocking_write (next);clean_tree_block (fs_info, next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
@@ -2829,7 +2829,7 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking (next); +btrfs_set_lock_blocking_write (next);clean_tree_block (fs_info, next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
Signature · 1 line
2.20.1
Re: [PATCH 4/6] btrfs: open code now trivial btrfs_set_lock_blocking
Looks good,
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
David Sterba
maintainerPATCH 5/6 btrfs: simplify waiting loop in btrfs_tree_lock
In reply to David SterbaCurrently, the number of readers and writers is checked and in case there are any, wait and redo the locks. There's some duplication before the branches go back to again label, eg. calling wait_event on blocking_readers twice.
The sequence is transformed
loop:
* wait for readers
* wait for writers
* write_lock
* check readers, unlock and wait for readers, loop
* check writers, unlock and wait for writers, loop
The new sequence is not exactly the same due to the simplification, for readers it's slightly faster. For the writers, original code does
* wait for writers
* (loop) wait for readers
* wait for writers -- again
while the new goes directly to the reader check. This should behave the same on a contended lock with multiple writers and readers, but can reduce number of times we're waiting on something.
--- fs/btrfs/locking.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
fs/btrfs/locking.c +2 -9
@@ -237,16 +237,9 @@ void btrfs_tree_lock(struct extent_buffer *eb)wait_event (eb->read_lock_wq,atomic_read (&eb->blocking_readers) ==0 );wait_event (eb->write_lock_wq,atomic_read (&eb->blocking_writers) ==0 );write_lock (&eb->lock); -if (atomic_read (&eb->blocking_readers)) { +if (atomic_read (&eb->blocking_readers) || +atomic_read (&eb->blocking_writers)) {write_unlock (&eb->lock); -wait_event (eb->read_lock_wq, -atomic_read (&eb->blocking_readers) ==0 ); - goto again; - } -if (atomic_read (&eb->blocking_writers)) { -write_unlock (&eb->lock); -wait_event (eb->write_lock_wq, -atomic_read (&eb->blocking_writers) ==0 ); goto again; }WARN_ON (atomic_read (&eb->spinning_writers));
Signature · 1 line
2.20.1
Re: [PATCH 5/6] btrfs: simplify waiting loop in btrfs_tree_lock
Looks good,
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
David Sterba
maintainerPATCH 6/6 btrfs: merge btrfs_set_lock_blocking_rw with it's caller
In reply to David SterbaThe last caller that does not have a fixed value of lock is btrfs_set_path_blocking, that actually does the same conditional swtich by the lock type so we can merge the branches together and remove the helper.
--- fs/btrfs/ctree.c | 13 ++++++++++--- fs/btrfs/locking.h | 12 ------------ 2 files changed, 10 insertions(+), 15 deletions(-)
fs/btrfs/ctree.c +10 -3
@@ -45,11 +45,18 @@ noinline void btrfs_set_path_blocking(struct btrfs_path *p)for (i =0 ; i <BTRFS_MAX_LEVEL ; i++) {if (!p->nodes[i] || !p->locks[i])continue ; -btrfs_set_lock_blocking_rw (p->nodes[i], p->locks[i]); -if (p->locks[i] ==BTRFS_READ_LOCK ) +/* +* If we currently have a spinning reader or writer lock this +* will bump the count of blocking holders and drop the +* spinlock. +*/ +if (p->locks[i] ==BTRFS_READ_LOCK ) { +btrfs_set_lock_blocking_read (p->nodes[i]); p->locks[i] =BTRFS_READ_LOCK_BLOCKING ; -else if (p->locks[i] ==BTRFS_WRITE_LOCK ) + }else if (p->locks[i] ==BTRFS_WRITE_LOCK ) { +btrfs_set_lock_blocking_write (p->nodes[i]); p->locks[i] =BTRFS_WRITE_LOCK_BLOCKING ; + } } }
fs/btrfs/locking.h +0 -12
@@ -39,16 +39,4 @@ static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw) BUG(); } -/* -* If we currently have a spinning reader or writer lock (indicated by the rw -* flag) this will bump the count of blocking holders and drop the spinlock. -*/ -static inline void btrfs_set_lock_blocking_rw (struct extent_buffer *eb,int rw) -{ -if (rw ==BTRFS_WRITE_LOCK ) -btrfs_set_lock_blocking_write (eb); -else if (rw ==BTRFS_READ_LOCK ) -btrfs_set_lock_blocking_read (eb); -} -#endif
Signature · 1 line
2.20.1
Re: [PATCH 6/6] btrfs: merge btrfs_set_lock_blocking_rw with it's caller
Looks good,
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850