diff mbox series

fs/btrfs: Use atomic_try_cmpxchg in free_extent_buffer

Message ID 20220809163633.8255-1-ubizjak@gmail.com (mailing list archive)
State New, archived
Headers show
Series fs/btrfs: Use atomic_try_cmpxchg in free_extent_buffer | expand

Commit Message

Uros Bizjak Aug. 9, 2022, 4:36 p.m. UTC
Use `atomic_try_cmpxchg(ptr, &old, new)` instead of
`atomic_cmpxchg(ptr, old, new) == old` in free_extent_buffer. This
has two benefits:

- The x86 cmpxchg instruction returns success in the ZF flag, so this
  change saves a compare after cmpxchg, as well as a related move
  instruction in the front of cmpxchg.

- atomic_try_cmpxchg implicitly assigns the *ptr value to &old when
  cmpxchg fails, enabling further code simplifications.

This patch has no functional change.

Cc: Chris Mason <clm@fb.com>
Cc: Josef Bacik <josef@toxicpanda.com>
Cc: David Sterba <dsterba@suse.com>
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
---
 fs/btrfs/extent_io.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Boris Burkov Aug. 15, 2022, 9:36 p.m. UTC | #1
On Tue, Aug 09, 2022 at 06:36:33PM +0200, Uros Bizjak wrote:
> Use `atomic_try_cmpxchg(ptr, &old, new)` instead of
> `atomic_cmpxchg(ptr, old, new) == old` in free_extent_buffer. This
> has two benefits:
> 
> - The x86 cmpxchg instruction returns success in the ZF flag, so this
>   change saves a compare after cmpxchg, as well as a related move
>   instruction in the front of cmpxchg.
> 
> - atomic_try_cmpxchg implicitly assigns the *ptr value to &old when
>   cmpxchg fails, enabling further code simplifications.
> 
> This patch has no functional change.
> 
> Cc: Chris Mason <clm@fb.com>
> Cc: Josef Bacik <josef@toxicpanda.com>
> Cc: David Sterba <dsterba@suse.com>
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Reviewed-by: Boris Burkov <boris@bur.io>
> ---
>  fs/btrfs/extent_io.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index bfae67c593c5..15ff196cbd6d 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c
> @@ -6328,18 +6328,16 @@ static int release_extent_buffer(struct extent_buffer *eb)
>  void free_extent_buffer(struct extent_buffer *eb)
>  {
>  	int refs;
> -	int old;
>  	if (!eb)
>  		return;
>  
> +	refs = atomic_read(&eb->refs);
>  	while (1) {
> -		refs = atomic_read(&eb->refs);
>  		if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
>  		    || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
>  			refs == 1))
>  			break;
> -		old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
> -		if (old == refs)
> +		if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1))
>  			return;
>  	}
>  
> -- 
> 2.37.1
>
David Sterba Aug. 18, 2022, 5:14 p.m. UTC | #2
On Tue, Aug 09, 2022 at 06:36:33PM +0200, Uros Bizjak wrote:
> Use `atomic_try_cmpxchg(ptr, &old, new)` instead of
> `atomic_cmpxchg(ptr, old, new) == old` in free_extent_buffer. This
> has two benefits:
> 
> - The x86 cmpxchg instruction returns success in the ZF flag, so this
>   change saves a compare after cmpxchg, as well as a related move
>   instruction in the front of cmpxchg.
> 
> - atomic_try_cmpxchg implicitly assigns the *ptr value to &old when
>   cmpxchg fails, enabling further code simplifications.
> 
> This patch has no functional change.
> 
> Cc: Chris Mason <clm@fb.com>
> Cc: Josef Bacik <josef@toxicpanda.com>
> Cc: David Sterba <dsterba@suse.com>
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>

It's not in a performance critical code but resulting code is slightly
more efficient. Added to misc-next, thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index bfae67c593c5..15ff196cbd6d 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -6328,18 +6328,16 @@  static int release_extent_buffer(struct extent_buffer *eb)
 void free_extent_buffer(struct extent_buffer *eb)
 {
 	int refs;
-	int old;
 	if (!eb)
 		return;
 
+	refs = atomic_read(&eb->refs);
 	while (1) {
-		refs = atomic_read(&eb->refs);
 		if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
 		    || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
 			refs == 1))
 			break;
-		old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
-		if (old == refs)
+		if (atomic_try_cmpxchg(&eb->refs, &refs, refs - 1))
 			return;
 	}