public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Oleh Derevenko <oleh.derevenko@gmail.com>
To: "H.J. Lu" <hjl.tools@gmail.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] x86: Optimize atomic_compare_and_exchange_[val|bool]_acq [BZ #28537]
Date: Wed, 3 Nov 2021 17:50:18 +0200	[thread overview]
Message-ID: <CAC1wWD2pTqw3OC4fPssOpdaR99G5AL8XuV9h8wGFep2xeyGbZg@mail.gmail.com> (raw)
In-Reply-To: <20211103150415.1211388-1-hjl.tools@gmail.com>

Hi, H.J. Lu

You may not perform plain reads on values you want to be atomic. This
results in undefined behavior.
For example, the compiler IS NOT obliged to perform the read with a
single CPU instruction -- of course it will not, but it is allowed to
read it in two halves and compare them separately. Or it may reuse
cached value from previous evaluations.
This is only the compiler level issue. Similar issues will arise at
CPU level with all the kind of memory coherency, caching and
instruction reordering.
Or if the value would cross a cache line boundary the plain read might
return half-updated value with the part from one cache line being new
and the other part being old.
Finally, I'm not following this thread and I have very little
knowledge on function purposes in the library but the "_acq" suffix
bears the impression that the memory operation has to exhibit the
acquire semantics. I.e., it has to fetch memory updates and make them
visible for the thread. But this is what you are trying to eliminate
with your patch.

On Wed, Nov 3, 2021 at 5:05 PM H.J. Lu via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> From the CPU's point of view, getting a cache line for writing is more
> expensive than reading.  See Appendix A.2 Spinlock in:
>
> https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/xeon-lock-scaling-analysis-paper.pdf
>
> The full compare and swap will grab the cache line exclusive and cause
> excessive cache line bouncing.  Check the current memory value first and
> return immediately if writing cache line may fail to reduce cache line
> bouncing on contended locks.
>
> This fixes BZ# 28537.
> ---
>  sysdeps/x86/atomic-machine.h | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/sysdeps/x86/atomic-machine.h b/sysdeps/x86/atomic-machine.h
> index 2692d94a92..92c7cf58b7 100644
> --- a/sysdeps/x86/atomic-machine.h
> +++ b/sysdeps/x86/atomic-machine.h
> @@ -73,9 +73,19 @@ typedef uintmax_t uatomic_max_t;
>  #define ATOMIC_EXCHANGE_USES_CAS       0
>
>  #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
> -  __sync_val_compare_and_swap (mem, oldval, newval)
> +  ({ __typeof (*(mem)) oldmem = *(mem), ret;                           \
> +     ret = (oldmem == (oldval)                                         \
> +           ? __sync_val_compare_and_swap (mem, oldval, newval)         \
> +           : oldmem);                                                  \
> +     ret; })
>  #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
> -  (! __sync_bool_compare_and_swap (mem, oldval, newval))
> +  ({ __typeof (*(mem)) old = *(mem);                                   \
> +     int ret;                                                          \
> +     if (old != (oldval))                                              \
> +       ret = 1;                                                                \
> +     else                                                              \
> +       ret = !__sync_bool_compare_and_swap (mem, oldval, newval);      \
> +     ret; })
>
>
>  #define __arch_c_compare_and_exchange_val_8_acq(mem, newval, oldval) \
> --
> 2.33.1
>


-- 

Oleh Derevenko

-- Skype with underscore

  parent reply	other threads:[~2021-11-03 15:50 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 15:04 H.J. Lu
2021-11-03 15:14 ` Andreas Schwab
2021-11-03 15:50 ` Oleh Derevenko [this message]
2021-11-03 16:59   ` Arjan van de Ven
2021-11-03 17:17     ` Andreas Schwab
2021-11-03 19:21       ` Arjan van de Ven
2021-11-03 19:48         ` H.J. Lu
2021-11-03 20:38       ` Oleh Derevenko
2021-11-03 22:12         ` H.J. Lu
2021-11-04  8:58           ` Oleh Derevenko
2021-11-04  9:44             ` Oleh Derevenko
2021-11-03 17:26     ` Oleh Derevenko
2021-11-03 17:30       ` Arjan van de Ven
2021-11-03 17:55         ` Oleh Derevenko
2021-11-03 19:22           ` Arjan van de Ven
2021-11-04 11:42     ` Oleh Derevenko
2021-11-04 14:15       ` Arjan van de Ven
2021-11-03 16:35 ` Florian Weimer
2021-11-03 19:13   ` H.J. Lu
2021-11-04 10:15     ` Florian Weimer
2021-11-04 14:31       ` H.J. Lu
2021-11-04 14:59         ` H.J. Lu
2021-11-03 17:25 ` Noah Goldstein

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=CAC1wWD2pTqw3OC4fPssOpdaR99G5AL8XuV9h8wGFep2xeyGbZg@mail.gmail.com \
    --to=oleh.derevenko@gmail.com \
    --cc=hjl.tools@gmail.com \
    --cc=libc-alpha@sourceware.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).