btrfs: use refcount_t type for extent buffers and cleanups
Subsystems (2)
cover btrfs: use refcount_t type for extent buffers and cleanups
From: Filipe Manana <[email protected]>
Switch from a bare atomic to refcount_t type to track extent buffer reference count and a couple cleanups. Details in the change logs.
Filipe Manana (3):
btrfs: reorganize logic at free_extent_buffer() for better readability
btrfs: add comment for optimization in free_extent_buffer()
btrfs: use refcount_t type for the extent buffer reference counter
fs/btrfs/ctree.c | 14 ++++----- fs/btrfs/extent-tree.c | 2 +- fs/btrfs/extent_io.c | 55 +++++++++++++++++++----------------- fs/btrfs/extent_io.h | 2 +- fs/btrfs/fiemap.c | 2 +- fs/btrfs/print-tree.c | 2 +- fs/btrfs/qgroup.c | 6 ++-- fs/btrfs/relocation.c | 4 +-- fs/btrfs/tree-log.c | 4 +-- fs/btrfs/zoned.c | 2 +- include/trace/events/btrfs.h | 2 +- 11 files changed, 49 insertions(+), 46 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/3 btrfs: reorganize logic at free_extent_buffer() for better readability
From: Filipe Manana <[email protected]>
It's hard to read the logic to break out of the while loop since it's a very long expression consisting of a logical or of two composite expressions, each one composed by a logical and. Further each one is also testing for the EXTENT_BUFFER_UNMAPPED bit, making it more verbose than necessary.
So change from this:
if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
|| (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
refs == 1))
break;To this:
if (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags)) {
if (refs == 1)
break;
} else if (refs <= 3) {
break;
}At least on x86_64 using gcc 9.3.0, this doesn't change the object size.
--- fs/btrfs/extent_io.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +6 -3
@@ -3487,10 +3487,13 @@ void free_extent_buffer(struct extent_buffer *eb) refs =atomic_read (&eb->refs);while (1 ) { -if ((!test_bit (EXTENT_BUFFER_UNMAPPED , &eb->bflags) && refs <=3 ) - || (test_bit (EXTENT_BUFFER_UNMAPPED , &eb->bflags) && - refs ==1 )) +if (test_bit (EXTENT_BUFFER_UNMAPPED , &eb->bflags)) { +if (refs ==1 ) +break ; + }else if (refs <=3 ) {break ; + } +if (atomic_try_cmpxchg (&eb->refs, &refs, refs -1 ))return ; }
Signature · 1 line
2.47.2
Re: [PATCH 1/3] btrfs: reorganize logic at free_extent_buffer() for better readability
Quoted 17 lines · On Tue, Jun 03, 2025 at 03:50:25PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
It's hard to read the logic to break out of the while loop since it's a very long expression consisting of a logical or of two composite expressions, each one composed by a logical and. Further each one is also testing for the
EXTENT_BUFFER_UNMAPPEDbit, making it more verbose than necessary.So change from this:
if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs == 1)) break;To this:
Quoted 8 lines
if (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags)) { if (refs == 1) break; } else if (refs <= 3) { break; }At least on
x86_64using gcc 9.3.0, this doesn't change the object size.
The object size remains the same probably due to function size alignments, the assembly code changed and I'd say for the better:
mov 0x10(%rbx),%rdx # eb->flags and $0x20,%edx jne free_extent_buffer cmp $0x3,%eax jle free_extent_buffer - mov 0x10(%rbx),%rdx lea -0x1(%rax),%edx lock cmpxchg %edx,0x2c(%rbx) jne free_extent_buffer pop %rbx ret - mov 0x10(%rbx),%rdx - and $0x20,%edx - je free_extent_buffer cmp $0x1,%eax jne free_extent_buffer lea 0x28(%rbx),%rdi
IOW it removes instructions that reloaded the eb->flags (offset 0x10/16), the new version reads it just once per loop. (The diff lacks jump targets and labels, complete view is in objdump.)
PATCH 2/3 btrfs: add comment for optimization in free_extent_buffer()
In reply to [email protected]From: Filipe Manana <[email protected]>
There's this special atomic compare and exchange logic which serves to avoid locking the extent buffers refs_lock spinlock and therefore reduce lock contention, so add a comment to make it more obvious.
--- fs/btrfs/extent_io.c | 1 + 1 file changed, 1 insertion(+)
fs/btrfs/extent_io.c +1 -0
@@ -3494,6 +3494,7 @@ void free_extent_buffer(struct extent_buffer *eb)break ; } +/* Optimization to avoid locking eb->refs_lock. */ if (atomic_try_cmpxchg (&eb->refs, &refs, refs -1 ))return ; }
Signature · 1 line
2.47.2
PATCH 3/3 btrfs: use refcount_t type for the extent buffer reference counter
In reply to [email protected]From: Filipe Manana <[email protected]>
Instead of using a bare atomic, use the refcount_t type, which despite being a structure that contains only an atomic, has an API that checks for underflows and other hazards. This doesn't change the size of the extent_buffer structure.
This removes the need to do things like this:
WARN_ON(atomic_read(&eb->refs) == 0);
if (atomic_dec_and_test(&eb->refs)) {
(...)
}And do just:
if (refcount_dec_and_test(&eb->refs)) {
(...)
}Since refcount_dec_and_test() already triggers a warning when we decrement a ref count that has a value of 0 (or below zero).
--- fs/btrfs/ctree.c | 14 +++++------ fs/btrfs/extent-tree.c | 2 +- fs/btrfs/extent_io.c | 45 ++++++++++++++++++------------------ fs/btrfs/extent_io.h | 2 +- fs/btrfs/fiemap.c | 2 +- fs/btrfs/print-tree.c | 2 +- fs/btrfs/qgroup.c | 6 ++--- fs/btrfs/relocation.c | 4 ++-- fs/btrfs/tree-log.c | 4 ++-- fs/btrfs/zoned.c | 2 +- include/trace/events/btrfs.h | 2 +- 11 files changed, 42 insertions(+), 43 deletions(-)
fs/btrfs/ctree.c +7 -7
@@ -198,7 +198,7 @@ struct extent_buffer *btrfs_root_node(struct btrfs_root *root)* the inc_not_zero dance and if it doesn't work then * synchronize_rcu and try again. */ -if (atomic_inc_not_zero (&eb->refs)) { +if (refcount_inc_not_zero (&eb->refs)) {rcu_read_unlock ();break ; }
@@ -560,7 +560,7 @@ int btrfs_force_cow_block(struct btrfs_trans_handle *trans,btrfs_abort_transaction (trans, ret); goto error_unlock_cow; } -atomic_inc (&cow->refs); +refcount_inc (&cow->refs);rcu_assign_pointer (root->node, cow); ret = btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
@@ -1092,7 +1092,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,/* update the path */ if (left) {if (btrfs_header_nritems (left) > orig_slot) { -atomic_inc (&left->refs); +refcount_inc (&left->refs);/* left was locked after cow */ path->nodes[level] = left; path->slots[level +1 ] -=1 ;
@@ -1696,7 +1696,7 @@ static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,if (p->search_commit_root) { b = root->commit_root; -atomic_inc (&b->refs); +refcount_inc (&b->refs); level =btrfs_header_level (b);/* * Ensure that all callers have set skip_locking when
@@ -2894,7 +2894,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,free_extent_buffer (old);add_root_to_dirty_list (root); -atomic_inc (&c->refs); +refcount_inc (&c->refs); path->nodes[level] = c; path->locks[level] =BTRFS_WRITE_LOCK ; path->slots[level] =0 ;
@@ -4451,7 +4451,7 @@ static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,root_sub_used_bytes (root); -atomic_inc (&leaf->refs); +refcount_inc (&leaf->refs); ret =btrfs_free_tree_block (trans,btrfs_root_id (root), leaf,0 ,1 );free_extent_buffer_stale (leaf);if (ret <0 )
@@ -4536,7 +4536,7 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,* for possible call to btrfs_del_ptr below */ slot = path->slots[1 ]; -atomic_inc (&leaf->refs); +refcount_inc (&leaf->refs);/* * We want to be able to at least push one item to the * left neighbour leaf, and that's the first item.
fs/btrfs/extent-tree.c +1 -1
@@ -6348,7 +6348,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,btrfs_assert_tree_write_locked (parent); parent_level =btrfs_header_level (parent); -atomic_inc (&parent->refs); +refcount_inc (&parent->refs); path->nodes[parent_level] = parent; path->slots[parent_level] =btrfs_header_nritems (parent);
fs/btrfs/extent_io.c +22 -23
166 lines not on this page · raw message
fs/btrfs/extent_io.h +1 -1
@@ -98,7 +98,7 @@ struct extent_buffer {void *addr;spinlock_t refs_lock; -atomic_t refs; +refcount_t refs;int read_mirror;/* >= 0 if eb belongs to a log tree, -1 otherwise */ s8 log_index;
fs/btrfs/fiemap.c +1 -1
@@ -320,7 +320,7 @@ static int fiemap_next_leaf_item(struct btrfs_inode *inode, struct btrfs_path *p* the cost of allocating a new one. */ ASSERT (test_bit (EXTENT_BUFFER_UNMAPPED , &clone->bflags)); -atomic_inc (&clone->refs); +refcount_inc (&clone->refs); ret =btrfs_next_leaf (inode->root, path);if (ret !=0 )
fs/btrfs/print-tree.c +1 -1
@@ -223,7 +223,7 @@ static void print_eb_refs_lock(const struct extent_buffer *eb) {#ifdef CONFIG_BTRFS_DEBUG btrfs_info (eb->fs_info,"refs %u lock_owner %u current %u" , -atomic_read (&eb->refs), eb->lock_owner, current->pid); +refcount_read (&eb->refs), eb->lock_owner, current->pid);#endif }
fs/btrfs/qgroup.c +3 -3
@@ -2338,7 +2338,7 @@ static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,btrfs_item_key_to_cpu (dst_path->nodes[dst_level], &key,0 );/* For src_path */ -atomic_inc (&src_eb->refs); +refcount_inc (&src_eb->refs); src_path->nodes[root_level] = src_eb; src_path->slots[root_level] = dst_path->slots[root_level]; src_path->locks[root_level] =0 ;
@@ -2571,7 +2571,7 @@ static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans, goto out; }/* For dst_path */ -atomic_inc (&dst_eb->refs); +refcount_inc (&dst_eb->refs); dst_path->nodes[level] = dst_eb; dst_path->slots[level] =0 ; dst_path->locks[level] =0 ;
@@ -2663,7 +2663,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,* walk back up the tree (adjusting slot pointers as we go) * and restart the search process. */ -atomic_inc (&root_eb->refs);/* For path */ +refcount_inc (&root_eb->refs);/* For path */ path->nodes[root_level] = root_eb; path->slots[root_level] =0 ; path->locks[root_level] =0 ;/* so release_path doesn't try to unlock */
fs/btrfs/relocation.c +2 -2
@@ -1524,7 +1524,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,if (btrfs_disk_key_objectid (&root_item->drop_progress) ==0 ) { level =btrfs_root_level (root_item); -atomic_inc (&reloc_root->node->refs); +refcount_inc (&reloc_root->node->refs); path->nodes[level] = reloc_root->node; path->slots[level] =0 ; }else {
@@ -4347,7 +4347,7 @@ int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, }btrfs_backref_drop_node_buffer (node); -atomic_inc (&cow->refs); +refcount_inc (&cow->refs); node->eb = cow; node->new_bytenr = cow->start;
fs/btrfs/tree-log.c +2 -2
@@ -2719,7 +2719,7 @@ static int walk_log_tree(struct btrfs_trans_handle *trans, level =btrfs_header_level (log->node); orig_level = level; path->nodes[level] = log->node; -atomic_inc (&log->node->refs); +refcount_inc (&log->node->refs); path->slots[level] =0 ;while (1 ) {
@@ -3683,7 +3683,7 @@ static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx)* Add extra ref to scratch eb so that it is not freed when callers * release the path, so we can reuse it later if needed. */ -atomic_inc (&ctx->scratch_eb->refs); +refcount_inc (&ctx->scratch_eb->refs);return 0 ; }
fs/btrfs/zoned.c +1 -1
@@ -2427,7 +2427,7 @@ void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,/* For the work */ btrfs_get_block_group (bg); -atomic_inc (&eb->refs); +refcount_inc (&eb->refs); bg->last_eb = eb;INIT_WORK (&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);queue_work (system_unbound_wq, &bg->zone_finish_work);
include/trace/events/btrfs.h +1 -1
@@ -1095,7 +1095,7 @@ TRACE_EVENT(btrfs_cow_block,TP_fast_assign_btrfs (root->fs_info, __entry->root_objectid = btrfs_root_id(root); __entry->buf_start = buf->start; - __entry->refs =atomic_read (&buf->refs); + __entry->refs =refcount_read (&buf->refs); __entry->cow_start = cow->start; __entry->buf_level =btrfs_header_level (buf); __entry->cow_level =btrfs_header_level (cow);
Signature · 1 line
2.47.2
Quoted 5 lines · On Tue, Jun 03, 2025 at 03:50:24PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
Switch from a bare atomic to
refcount_ttype to track extent buffer reference count and a couple cleanups. Details in the change logs.
Quoted 21 lines
Filipe Manana (3):
btrfs: reorganize logic atfree_extent_buffer()for better readability
btrfs: add comment for optimization infree_extent_buffer()
btrfs: userefcount_ttype for the extent buffer reference counterfs/btrfs/ctree.c | 14 ++++----- fs/btrfs/extent-tree.c | 2 +- fs/btrfs/extent_io.c | 55 +++++++++++++++++++----------------- fs/btrfs/extent_io.h | 2 +- fs/btrfs/fiemap.c | 2 +- fs/btrfs/print-tree.c | 2 +- fs/btrfs/qgroup.c | 6 ++-- fs/btrfs/relocation.c | 4 +-- fs/btrfs/tree-log.c | 4 +-- fs/btrfs/zoned.c | 2 +- include/trace/events/btrfs.h | 2 +- 11 files changed, 49 insertions(+), 46 deletions(-)Signature · 1 line
2.47.2
Quoted 4 lines · On Tue, Jun 03, 2025 at 03:50:24PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
Switch from a bare atomic to
refcount_ttype to track extent buffer reference count and a couple cleanups. Details in the change logs.
Right, apart from the atomic xchg it's a refcount so using the API makes sense.
Quoted 4 lines
Filipe Manana (3):
btrfs: reorganize logic atfree_extent_buffer()for better readability
btrfs: add comment for optimization infree_extent_buffer()
btrfs: userefcount_ttype for the extent buffer reference counter