All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mark Rutland <mark.rutland@arm.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: will@kernel.org, boqun.feng@gmail.com,
	linux-kernel@vger.kernel.org, x86@kernel.org, elver@google.com,
	keescook@chromium.org, hch@infradead.org,
	torvalds@linux-foundation.org, axboe@kernel.dk
Subject: Re: [PATCH v2 5/9] atomic,x86: Implement atomic_dec_and_test_overflow()
Date: Mon, 13 Dec 2021 11:04:27 +0000	[thread overview]
Message-ID: <Ybcou19VF7Vf0kP0@FVFF77S0Q05N> (raw)
In-Reply-To: <20211210162313.599116743@infradead.org>

On Fri, Dec 10, 2021 at 05:16:23PM +0100, Peter Zijlstra wrote:
> Provide a better implementation of atomic_{dec,dec_and_test}_overflow() by
> making use of the atomic-op condition codes.
> 
> This further improves the fast path code:
> 
>     a980:       b8 ff ff ff ff          mov    $0xffffffff,%eax
>     a985:       f0 0f c1 07             lock xadd %eax,(%rdi)
>     a989:       83 e8 01                sub    $0x1,%eax
>     a98c:       78 20                   js     a9ae <ring_buffer_put+0x2e>
>     a98e:       74 01                   je     a991 <ring_buffer_put+0x11>
>     a990:       c3                      ret
> 
> to:
> 
>     a950:       f0 ff 0f                lock decl (%rdi)
>     a953:       7c 20                   jl     a975 <ring_buffer_put+0x25>
>     a955:       74 01                   je     a958 <ring_buffer_put+0x8>
>     a957:       c3                      ret

As a heads-up, for arm64 we probably won't need to roll our own since the LSE
atomics don't have flag-setting versions, and the compiler-generated
comparisons and conditional branches can be better optimized per caller.

Otherwise, I'm going to ignore this patch since I'm not qualified to review x86
asm. :)

Mark.

> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  arch/x86/include/asm/atomic.h |   23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
> 
> --- a/arch/x86/include/asm/atomic.h
> +++ b/arch/x86/include/asm/atomic.h
> @@ -263,6 +263,29 @@ static __always_inline int arch_atomic_f
>  }
>  #define arch_atomic_fetch_xor arch_atomic_fetch_xor
>  
> +#define arch_atomic_dec_overflow(_v, _label)				\
> +	asm_volatile_goto(LOCK_PREFIX "decl %[var]\n\t"			\
> +			  "jle %l1"					\
> +			  : : [var] "m" ((_v)->counter)			\
> +			  : "memory"					\
> +			  : _label)
> +
> +#define arch_atomic_dec_and_test_overflow(_v, _label)			\
> +({									\
> +	__label__ __zero;						\
> +	__label__ __out;						\
> +	bool __ret = false;						\
> +	asm_volatile_goto(LOCK_PREFIX "decl %[var]\n\t"			\
> +			  "jl %l2\n\t"					\
> +			  "je %l[__zero]"				\
> +			  : : [var] "m" ((_v)->counter)			\
> +			  : "memory"					\
> +			  : __zero, _label);				\
> +	goto __out;							\
> +__zero:	__ret = true;							\
> +__out:	__ret;								\
> +})
> +
>  #ifdef CONFIG_X86_32
>  # include <asm/atomic64_32.h>
>  #else
> 
> 

  reply	other threads:[~2021-12-13 11:04 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-10 16:16 [PATCH v2 0/9] refcount: Improve code-gen Peter Zijlstra
2021-12-10 16:16 ` [PATCH v2 1/9] atomic: Prepare scripts for macro ops Peter Zijlstra
2021-12-10 17:27   ` Mark Rutland
2021-12-10 17:43   ` Marco Elver
2021-12-10 16:16 ` [PATCH v2 2/9] atomic: Add xchg.tbl Peter Zijlstra
2021-12-13  9:50   ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 3/9] atomic: Introduce atomic_{inc,dec,dec_and_test}_overflow() Peter Zijlstra
2021-12-13 10:06   ` Mark Rutland
2021-12-13 10:57     ` Peter Zijlstra
2021-12-13 10:59     ` Peter Zijlstra
2021-12-13 11:09       ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 4/9] refcount: Use atomic_*_overflow() Peter Zijlstra
2021-12-13 10:35   ` Mark Rutland
2021-12-10 16:16 ` [PATCH v2 5/9] atomic,x86: Implement atomic_dec_and_test_overflow() Peter Zijlstra
2021-12-13 11:04   ` Mark Rutland [this message]
2021-12-10 16:16 ` [PATCH v2 6/9] refcount: Fix refcount_dec_not_one() Peter Zijlstra
2021-12-10 16:16 ` [PATCH v2 7/9] refcount: Prepare for atomic_*_overflow() offsets Peter Zijlstra
2021-12-10 16:16 ` [PATCH v2 8/9] atomic,x86: Alternative atomic_*_overflow() scheme Peter Zijlstra
2021-12-10 16:53   ` Linus Torvalds
2021-12-10 17:27     ` Linus Torvalds
2021-12-17  3:38     ` Herbert Xu
2021-12-13 16:43   ` Peter Zijlstra
2021-12-13 17:29     ` Marco Elver
2021-12-13 18:11     ` Linus Torvalds
2021-12-13 18:18       ` Marco Elver
2021-12-13 18:24         ` Linus Torvalds
2021-12-13 19:35           ` Marco Elver
2021-12-13 18:21       ` Linus Torvalds
2021-12-10 16:16 ` [PATCH v2 9/9] refcount: Optimize __refcount_add_not_zero(.i=1) Peter Zijlstra
2021-12-10 19:37 ` [PATCH v2 0/9] refcount: Improve code-gen Peter Zijlstra
2021-12-13 12:15 ` [PATCH v2 10/9] atomic: Document the atomic_{}_overflow() functions Peter Zijlstra
2021-12-13 12:20 ` [PATCH v2 0/9] refcount: Improve code-gen Peter Zijlstra
2021-12-13 14:42   ` Marco Elver
2021-12-13 16:11     ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Ybcou19VF7Vf0kP0@FVFF77S0Q05N \
    --to=mark.rutland@arm.com \
    --cc=axboe@kernel.dk \
    --cc=boqun.feng@gmail.com \
    --cc=elver@google.com \
    --cc=hch@infradead.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.