All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8
@ 2018-10-26 16:03 Junchang Wang
  2018-10-26 20:58 ` Paul E. McKenney
  0 siblings, 1 reply; 2+ messages in thread
From: Junchang Wang @ 2018-10-26 16:03 UTC (permalink / raw)
  To: paulmck, akiyks, perfbook; +Cc: Junchang Wang

If the 4th parameter (field *weak*) of __atomic_compare_exchange_n is set, it
may refuse to perform the CAS operation and return a failure, even if the value
of field *old* is equal to the content of the specified memory location. This
happens in architectures where LL/SC is used to emulate CAS primitive (e.g., PPC
and ARM).

This patch returns the *old* value if __atomic_compare_exchange_n has been
successfully performed; otherwise, *old*+1 is returned, which forces the caller
to retry the CAS loop.

Signed-off-by: Junchang Wang <junchangwang@gmail.com>
Signed-off-by: kira Yokosawa <akiyks@gmail.com>
---
 CodeSamples/api-pthreads/api-gcc.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/CodeSamples/api-pthreads/api-gcc.h b/CodeSamples/api-pthreads/api-gcc.h
index 1dd26ca..3afe340 100644
--- a/CodeSamples/api-pthreads/api-gcc.h
+++ b/CodeSamples/api-pthreads/api-gcc.h
@@ -168,9 +168,8 @@ struct __xchg_dummy {
 ({ \
 	typeof(*ptr) _____actual = (o); \
 	\
-	(void)__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
-					  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
-	_____actual; \
+	__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
+			__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? (o) : (o)+1; \
 })
 
 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8
  2018-10-26 16:03 [PATCH] atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8 Junchang Wang
@ 2018-10-26 20:58 ` Paul E. McKenney
  0 siblings, 0 replies; 2+ messages in thread
From: Paul E. McKenney @ 2018-10-26 20:58 UTC (permalink / raw)
  To: Junchang Wang; +Cc: akiyks, perfbook

On Sat, Oct 27, 2018 at 12:03:25AM +0800, Junchang Wang wrote:
> If the 4th parameter (field *weak*) of __atomic_compare_exchange_n is set, it
> may refuse to perform the CAS operation and return a failure, even if the value
> of field *old* is equal to the content of the specified memory location. This
> happens in architectures where LL/SC is used to emulate CAS primitive (e.g., PPC
> and ARM).
> 
> This patch returns the *old* value if __atomic_compare_exchange_n has been
> successfully performed; otherwise, *old*+1 is returned, which forces the caller
> to retry the CAS loop.
> 
> Signed-off-by: Junchang Wang <junchangwang@gmail.com>
> Signed-off-by: kira Yokosawa <akiyks@gmail.com>

Very good, queued and pushed with adjustments to the commit log above,
thank you!!!

Please let me know if further adjustments are required.

							Thanx, Paul

> ---
>  CodeSamples/api-pthreads/api-gcc.h | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/CodeSamples/api-pthreads/api-gcc.h b/CodeSamples/api-pthreads/api-gcc.h
> index 1dd26ca..3afe340 100644
> --- a/CodeSamples/api-pthreads/api-gcc.h
> +++ b/CodeSamples/api-pthreads/api-gcc.h
> @@ -168,9 +168,8 @@ struct __xchg_dummy {
>  ({ \
>  	typeof(*ptr) _____actual = (o); \
>  	\
> -	(void)__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
> -					  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
> -	_____actual; \
> +	__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
> +			__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? (o) : (o)+1; \
>  })
>  
>  static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
> -- 
> 2.7.4
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-27  5:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-26 16:03 [PATCH] atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8 Junchang Wang
2018-10-26 20:58 ` Paul E. McKenney

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.