All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mark Rutland <mark.rutland@arm.com>
To: Junru Shen <hhusjrsjr@gmail.com>
Cc: Will Deacon <will@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] atomic: Put the fetching of the old value into the loop when doing atomic CAS
Date: Thu, 24 Feb 2022 10:27:54 +0000	[thread overview]
Message-ID: <YhddqvNzc5Hz7Ogj@lakrids> (raw)
In-Reply-To: <20220224082438.580191-1-hhusjrsjr@gmail.com>

On Thu, Feb 24, 2022 at 04:24:38PM +0800, Junru Shen wrote:
> Put the acquisition of the expected value inside the loop to prevent
> an infinite loop when it does not match.

I suspect you've found this by inspection, as I don't beleive this can
happen. See below.

> Signed-off-by: Junru Shen <hhusjrsjr@gmail.com>
> ---
>  arch/x86/include/asm/atomic64_64.h | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/include/asm/atomic64_64.h b/arch/x86/include/asm/atomic64_64.h
> index 7886d0578..3df04c44c 100644
> --- a/arch/x86/include/asm/atomic64_64.h
> +++ b/arch/x86/include/asm/atomic64_64.h
> @@ -207,9 +207,10 @@ static inline void arch_atomic64_and(s64 i, atomic64_t *v)
>  
>  static inline s64 arch_atomic64_fetch_and(s64 i, atomic64_t *v)
>  {
> -	s64 val = arch_atomic64_read(v);
> +	s64 val;
>  
>  	do {
> +		val = arch_atomic64_read(v);
>  	} while (!arch_atomic64_try_cmpxchg(v, &val, val & i));
  	                                       ^^^^
See this bit above? ----------------------------'

If arch_atomic64_try_cmpxchg() fails, it writes the value in memory back to
this address, so it has already done the equivalent of arch_atomic64_read(v).

If you're seing this go wrong, it implies that arch_atomic64_try_cmpxchg() is
being mis-compiled, so please provide an example and the disassembly.

Likewise for the other instances below.

Thanks,
Mark.

>  	return val;
>  }
> @@ -225,9 +226,10 @@ static inline void arch_atomic64_or(s64 i, atomic64_t *v)
>  
>  static inline s64 arch_atomic64_fetch_or(s64 i, atomic64_t *v)
>  {
> -	s64 val = arch_atomic64_read(v);
> +	s64 val;
>  
>  	do {
> +		val = arch_atomic64_read(v);
>  	} while (!arch_atomic64_try_cmpxchg(v, &val, val | i));
>  	return val;
>  }
> @@ -243,9 +245,10 @@ static inline void arch_atomic64_xor(s64 i, atomic64_t *v)
>  
>  static inline s64 arch_atomic64_fetch_xor(s64 i, atomic64_t *v)
>  {
> -	s64 val = arch_atomic64_read(v);
> +	s64 val;
>  
>  	do {
> +		val = arch_atomic64_read(v);
>  	} while (!arch_atomic64_try_cmpxchg(v, &val, val ^ i));
>  	return val;
>  }
> -- 
> 2.30.2
> 

  parent reply	other threads:[~2022-02-24 10:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-24  8:24 [PATCH] atomic: Put the fetching of the old value into the loop when doing atomic CAS Junru Shen
2022-02-24 10:13 ` Boqun Feng
2022-02-24 10:27 ` Mark Rutland [this message]
2022-02-24 12:02 ` Peter Zijlstra
  -- strict thread matches above, loose matches on Subject: below --
2022-02-24  8:24 Junru Shen

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=YhddqvNzc5Hz7Ogj@lakrids \
    --to=mark.rutland@arm.com \
    --cc=boqun.feng@gmail.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hhusjrsjr@gmail.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --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.