btrfs: fix inefficiencies when reading extent buffers and some cleanups
cover btrfs: fix inefficiencies when reading extent buffers and some cleanups
From: Filipe Manana <[email protected]>
Fix a couple inefficiencies when reading an extent buffer while searching a btree plus a couple cleanups in the same area. Spotted while working on other stuff, but this is separate and independent enough to be in its own small patchset.
Filipe Manana (4):
btrfs: avoid unnecessary btree search restarts when reading node
btrfs: release upper nodes when reading stale btree node from disk
btrfs: update outdated comment for read_block_for_search()
btrfs: remove trivial wrapper btrfs_read_buffer()
fs/btrfs/ctree.c | 57 ++++++++++++++++++++++++++++++--------------- fs/btrfs/disk-io.c | 16 ++++--------- fs/btrfs/disk-io.h | 4 ++-- fs/btrfs/qgroup.c | 2 +- fs/btrfs/tree-log.c | 9 +++---- 5 files changed, 50 insertions(+), 38 deletions(-)
Signature · 1 line
2.33.0
PATCH 1/4 btrfs: avoid unnecessary btree search restarts when reading node
From: Filipe Manana <[email protected]>
When reading a btree node, at read_block_for_search(), if we don't find the node's (or leaf) extent buffer in the cache, we will read it from disk. Since that requires waiting on IO, we release all upper level nodes from our path before reading the target node/leaf, and then return -EAGAIN to the caller, which will make the caller restart the while btree search.
However we are causing the restart of btree search even for cases where it is not necessary:
1) We have a path with ->skip_locking set to true, typically when doing a search on a commit root, so we are never holding locks on any node;
2) We are doing a read search (the "ins_len" argument passed to btrfs_search_slot() is 0), or we are doing a search to modify an existing key (the "cow" argument passed to btrfs_search_slot() has a value of 1 and "ins_len" is 0), in which case we never hold locks for upper level nodes;
3) We are doing a search to insert or delete a key, in which case we may or may not have upper level nodes locked. That depends on the current minimum write lock levels at btrfs_search_slot(), if we had to split or merge parent nodes, if we had to COW upper level nodes and if we ever visited slot 0 of an upper level node. It's still common to not have upper level nodes locked, but our current node must be at least at level 1, for insertions, or at least at level 2 for deletions. In these cases when we have locks on upper level nodes, they are always write locks.
These cases where we are not holding locks on upper level nodes far outweigh the cases where we are holding locks, so it's completely wasteful to retry the whole search when we have no upper nodes locked.
So change the logic to not return -EAGAIN, and make the caller retry the search, when we don't have the parent node locked - when it's not locked it means no other upper level nodes are locked as well.
--- fs/btrfs/ctree.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
fs/btrfs/ctree.c +19 -11
@@ -1447,19 +1447,22 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,return 0 ; } -/* -* reduce lock contention at high levels -* of the btree by dropping locks before -* we read. Don't release the lock on the current -* level because we need to walk this node to figure -* out which blocks to read. -*/ -btrfs_unlock_up_safe (p, level +1 ); +if ((level +1 <BTRFS_MAX_LEVEL ) && p->locks[level +1 ]) { +/* +* Reduce lock contention at high levels of the btree by +* dropping locks before we read. Don't release the lock +* on the current level because we need to walk this node +* to figure out which blocks to read. +*/ +btrfs_unlock_up_safe (p, level +1 ); + ret = -EAGAIN ; + }else { + ret =0 ; + }if (p->reada !=READA_NONE )reada_for_search (fs_info, p, level, slot, key->objectid); - ret = -EAGAIN ; tmp =read_tree_block (fs_info, blocknr, root->root_key.objectid, gen, parent_level -1 , &first_key);if (IS_ERR (tmp)) {
@@ -1474,9 +1477,14 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,*/ if (!extent_buffer_uptodate (tmp)) ret = -EIO ; -free_extent_buffer (tmp); -btrfs_release_path (p); +if (ret ==0 ) { + *eb_ret = tmp; + }else { +free_extent_buffer (tmp); +btrfs_release_path (p); + } +return ret; }
Signature · 1 line
2.33.0
PATCH 2/4 btrfs: release upper nodes when reading stale btree node from disk
In reply to [email protected]From: Filipe Manana <[email protected]>
When reading a btree node (or leaf), at read_block_for_search(), if we can't find its extent buffer in the cache (the fs_info->buffer_radix radix tree), then we unlock all upper level nodes before reading the btree node/leaf from disk, to prevent blocking other tasks for too long.
However if we find that the extent buffer is in the cache but it is not up to date, we don't unlock upper level nodes before reading it from disk, potentially blocking other tasks on upper level nodes for too long.
Fix this inconsistent behaviour by unlocking upper level nodes if we need to read a node/leaf from disk because its in-memory extent buffer is not up to date. If we unlocked upper level nodes then we must return -EAGAIN to the caller, just like the case where the extent buffer is not cached in memory. And like that case, we determine if upper level nodes are locked by checking only if the parent node is locked - if it isn't, then no other upper level nodes are locked.
This is actually a rare case, as if we have an extent buffer in memory, it typically has the uptodate flag set and passes all the checks done by btrfs_buffer_uptodate().
--- fs/btrfs/ctree.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
fs/btrfs/ctree.c +19 -9
@@ -1409,12 +1409,21 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,struct btrfs_key first_key;int ret;int parent_level; +bool unlock_up; + unlock_up = ((level +1 <BTRFS_MAX_LEVEL ) && p->locks[level +1 ]); blocknr =btrfs_node_blockptr (*eb_ret, slot); gen =btrfs_node_ptr_generation (*eb_ret, slot); parent_level =btrfs_header_level (*eb_ret);btrfs_node_key_to_cpu (*eb_ret, &first_key, slot); +/* +* If we need to read an extent buffer from disk and we are holding locks +* on upper level nodes, we unlock all the upper nodes before reading the +* extent buffer, and then return -EAGAIN to the caller as it needs to +* restart the search. We don't release the lock on the current level +* because we need to walk this node to figure out which blocks to read. +*/ tmp =find_extent_buffer (fs_info, blocknr);if (tmp) {if (p->reada ==READA_FORWARD_ALWAYS )
@@ -1436,6 +1445,9 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,return 0 ; } +if (unlock_up) +btrfs_unlock_up_safe (p, level +1 ); +/* now we're allowed to do a blocking uptodate check */ ret =btrfs_read_buffer (tmp, gen, parent_level -1 , &first_key);if (ret) {
@@ -1443,17 +1455,14 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,btrfs_release_path (p);return -EIO ; } - *eb_ret = tmp; -return 0 ; + +if (unlock_up) + ret = -EAGAIN ; + + goto out; } -if ((level +1 <BTRFS_MAX_LEVEL ) && p->locks[level +1 ]) { -/* -* Reduce lock contention at high levels of the btree by -* dropping locks before we read. Don't release the lock -* on the current level because we need to walk this node -* to figure out which blocks to read. -*/ +if (unlock_up) {btrfs_unlock_up_safe (p, level +1 ); ret = -EAGAIN ; }else {
@@ -1478,6 +1487,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,if (!extent_buffer_uptodate (tmp)) ret = -EIO ; +out:if (ret ==0 ) { *eb_ret = tmp; }else {
Signature · 1 line
2.33.0
PATCH 3/4 btrfs: update outdated comment for read_block_for_search()
In reply to [email protected]From: Filipe Manana <[email protected]>
The comment at the top of read_block_for_search() is very outdated, as it refers to the blocking versus spinning path locking modes. We no longer have these two locking modes after we switched the btree locks from custom code to rw semaphores. So update the comment to stop referring to the blocking mode and put it more up to date.
--- fs/btrfs/ctree.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
fs/btrfs/ctree.c +6 -5
@@ -1390,12 +1390,13 @@ static noinline void unlock_up(struct btrfs_path *path, int level, }/* -* helper function for btrfs_search_slot. The goal is to find a block -* in cache without setting the path to blocking. If we find the block -* we return zero and the path is unchanged. +* Helper function for btrfs_search_slot() and other functions that do a search +* on a btree. The goal is to find a tree block in the cache (the radix tree at +* fs_info->buffer_radix), but if we can't find it, or it's not up to date, read +* its pages from disk. * -* If we can't find the block, we set the path blocking and do some -* reada. -EAGAIN is returned and the search must be repeated. +* Returns -EAGAIN, with the path unlocked, if the caller needs to repeat the +* whole btree search, starting again from the current root node. */ static int read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
Signature · 1 line
2.33.0
PATCH 4/4 btrfs: remove trivial wrapper btrfs_read_buffer()
In reply to [email protected]From: Filipe Manana <[email protected]>
The function btrfs_read_buffer() is useless, it just calls btree_read_extent_buffer_pages() with exactly the same arguments.
So remove it and rename btree_read_extent_buffer_pages() to btrfs_read_extent_buffer(), which is a shorter name, has the "btrfs_" prefix (since it's used outside disk-io.c) and the name is clear enough about what it does.
--- fs/btrfs/ctree.c | 2 +- fs/btrfs/disk-io.c | 16 ++++------------ fs/btrfs/disk-io.h | 4 ++-- fs/btrfs/qgroup.c | 2 +- fs/btrfs/tree-log.c | 9 +++++---- 5 files changed, 13 insertions(+), 20 deletions(-)
fs/btrfs/ctree.c +1 -1
@@ -1450,7 +1450,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,btrfs_unlock_up_safe (p, level +1 );/* now we're allowed to do a blocking uptodate check */ - ret =btrfs_read_buffer (tmp, gen, parent_level -1 , &first_key); + ret =btrfs_read_extent_buffer (tmp, gen, parent_level -1 , &first_key);if (ret) {free_extent_buffer (tmp);btrfs_release_path (p);
fs/btrfs/disk-io.c +4 -12
@@ -374,9 +374,9 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level,* @level: expected level, mandatory check * @first_key: expected key of first slot, skip check if NULL */ -static int btree_read_extent_buffer_pages (struct extent_buffer *eb, -u64 parent_transid,int level, -struct btrfs_key *first_key) +int btrfs_read_extent_buffer (struct extent_buffer *eb, +u64 parent_transid,int level, +struct btrfs_key *first_key) {struct btrfs_fs_info *fs_info = eb->fs_info;struct extent_io_tree *io_tree;
@@ -1117,8 +1117,7 @@ struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,if (IS_ERR (buf))return buf; - ret =btree_read_extent_buffer_pages (buf, parent_transid, - level, first_key); + ret =btrfs_read_extent_buffer (buf, parent_transid, level, first_key);if (ret) {free_extent_buffer_stale (buf);return ERR_PTR (ret);
@@ -4850,13 +4849,6 @@ void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)__btrfs_btree_balance_dirty (fs_info,0 ); } -int btrfs_read_buffer (struct extent_buffer *buf,u64 parent_transid,int level, -struct btrfs_key *first_key) -{ -return btree_read_extent_buffer_pages (buf, parent_transid, - level, first_key); -} -static void btrfs_error_commit_super (struct btrfs_fs_info *fs_info) {/* cleanup FS via transaction */
fs/btrfs/disk-io.h +2 -2
@@ -120,8 +120,8 @@ void btrfs_put_root(struct btrfs_root *root);void btrfs_mark_buffer_dirty (struct extent_buffer *buf);int btrfs_buffer_uptodate (struct extent_buffer *buf,u64 parent_transid,int atomic); -int btrfs_read_buffer (struct extent_buffer *buf,u64 parent_transid,int level, -struct btrfs_key *first_key); +int btrfs_read_extent_buffer (struct extent_buffer *buf,u64 parent_transid, +int level,struct btrfs_key *first_key);blk_status_t btrfs_bio_wq_end_io (struct btrfs_fs_info *info,struct bio *bio,enum btrfs_wq_endio_type metadata); blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio,
fs/btrfs/qgroup.c +1 -1
@@ -2290,7 +2290,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,return 0 ;if (!extent_buffer_uptodate (root_eb)) { - ret =btrfs_read_buffer (root_eb, root_gen, root_level,NULL ); + ret =btrfs_read_extent_buffer (root_eb, root_gen, root_level,NULL );if (ret) goto out; }
fs/btrfs/tree-log.c +5 -4
@@ -333,7 +333,7 @@ static int process_one_buffer(struct btrfs_root *log,* pin down any logged extents, so we have to read the block. */ if (btrfs_fs_incompat (fs_info,MIXED_GROUPS )) { - ret =btrfs_read_buffer (eb, gen, level,NULL ); + ret =btrfs_read_extent_buffer (eb, gen, level,NULL );if (ret)return ret; }
@@ -2575,7 +2575,7 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,int i;int ret; - ret =btrfs_read_buffer (eb, gen, level,NULL ); + ret =btrfs_read_extent_buffer (eb, gen, level,NULL );if (ret)return ret;
@@ -2786,7 +2786,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, path->slots[*level]++;if (wc->free) { - ret =btrfs_read_buffer (next, ptr_gen, + ret =btrfs_read_extent_buffer (next, ptr_gen, *level -1 , &first_key);if (ret) {free_extent_buffer (next);
@@ -2815,7 +2815,8 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,free_extent_buffer (next);continue ; } - ret =btrfs_read_buffer (next, ptr_gen, *level -1 , &first_key); + ret =btrfs_read_extent_buffer (next, ptr_gen, *level -1 , + &first_key);if (ret) {free_extent_buffer (next);return ret;
Signature · 1 line
2.33.0
Quoted 13 lines · On Fri, Mar 11, 2022 at 11:35:30AM +0000, [email protected] wrote:
From: Filipe Manana <[email protected]>
Fix a couple inefficiencies when reading an extent buffer while searching a btree plus a couple cleanups in the same area. Spotted while working on other stuff, but this is separate and independent enough to be in its own small patchset.
Filipe Manana (4):
btrfs: avoid unnecessary btree search restarts when reading node
btrfs: release upper nodes when reading stale btree node from disk
btrfs: update outdated comment forread_block_for_search()
btrfs: remove trivial wrapperbtrfs_read_buffer()
Quoted 10 lines
fs/btrfs/ctree.c | 57 ++++++++++++++++++++++++++++++--------------- fs/btrfs/disk-io.c | 16 ++++--------- fs/btrfs/disk-io.h | 4 ++-- fs/btrfs/qgroup.c | 2 +- fs/btrfs/tree-log.c | 9 +++---- 5 files changed, 50 insertions(+), 38 deletions(-)Signature · 1 line
2.33.0
You can add
to the series, thanks,
Josef
Quoted 7 lines · On Fri, Mar 11, 2022 at 11:35:30AM +0000, [email protected] wrote:
From: Filipe Manana <[email protected]>
Fix a couple inefficiencies when reading an extent buffer while searching a btree plus a couple cleanups in the same area. Spotted while working on other stuff, but this is separate and independent enough to be in its own small patchset.
Quoted 5 lines
Filipe Manana (4):
btrfs: avoid unnecessary btree search restarts when reading node
btrfs: release upper nodes when reading stale btree node from disk
btrfs: update outdated comment forread_block_for_search()
btrfs: remove trivial wrapperbtrfs_read_buffer()
Added to misc-next, thanks.