From: Serge Guelton <sguelton@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Kevin Wolf <kwolf@redhat.com>,
fweimer@redhat.com, thuth@redhat.com,
Daniel Berrange <berrange@redhat.com>,
qemu-block@nongnu.org,
Richard Henderson <richard.henderson@linaro.org>,
qemu-devel@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>, Fam Zheng <fam@euphon.net>,
Warner Losh <imp@bsdimp.com>
Subject: Re: [RFC v2 1/4] tls: add macros for coroutine-safe TLS variables
Date: Fri, 3 Dec 2021 07:24:14 +0100 [thread overview]
Message-ID: <20211203062414.GA1106@sguelton.remote.csb> (raw)
In-Reply-To: <CAFEAcA-QU_PERcLCf3WpTc_mTU6LymEaHqVJTtahGRD8H6oT9A@mail.gmail.com>
On Thu, Dec 02, 2021 at 02:44:42PM +0000, Peter Maydell wrote:
> On Wed, 1 Dec 2021 at 17:19, Stefan Hajnoczi <stefanha@redhat.com> wrote:
> >
> > Compiler optimizations can cache TLS values across coroutine yield
> > points, resulting in stale values from the previous thread when a
> > coroutine is re-entered by a new thread.
> >
> > Serge Guelton developed an __attribute__((noinline)) wrapper and tested
> > it with clang and gcc. I formatted his idea according to QEMU's coding
> > style and wrote documentation.
>
> > +#ifdef QEMU_CO_TLS_ADDR
> > +#define QEMU_DEFINE_STATIC_CO_TLS(type, var) \
> > + __thread type co_tls_##var; \
> > + static inline type get_##var(void) \
> > + { type *p; QEMU_CO_TLS_ADDR(p, co_tls_##var); return *p; } \
> > + static inline void set_##var(type v) \
> > + { type *p; QEMU_CO_TLS_ADDR(p, co_tls_##var); *p = v; } \
> > + static inline type *get_ptr_##var(void) \
> > + { type *p; QEMU_CO_TLS_ADDR(p, co_tls_##var); return p; }
> > +#else
> > +#define QEMU_DEFINE_STATIC_CO_TLS(type, var) \
> > + static __thread type co_tls_##var; \
> > + static __attribute__((noinline, unused)) type get_##var(void) \
> > + { return co_tls_##var; } \
> > + static __attribute__((noinline, unused)) void set_##var(type v) \
> > + { co_tls_##var = v; } \
> > + static __attribute__((noinline, unused)) type *get_ptr_##var(void) \
> > + { return &co_tls_##var; }
> > +#endif
>
> My compiler-developer colleagues present the following case where
> 'noinline' is not sufficient for the compiler to definitely
> use different values of the address-of-the-TLS-var across an
> intervening function call:
>
> __thread int i;
>
> __attribute__((noinline)) long get_ptr_i()
> {
> return (long)&i;
> }
>
> void switcher();
>
> int g()
> {
> long a = get_ptr_i();
> switcher();
> return a == get_ptr_i();
> }
You can also force an extra mov through `volatile` as in
https://godbolt.org/z/hWvdb7o9G
next prev parent reply other threads:[~2021-12-03 6:26 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-01 17:01 [RFC v2 0/4] tls: add macros for coroutine-safe TLS variables Stefan Hajnoczi
2021-12-01 17:01 ` [RFC v2 1/4] " Stefan Hajnoczi
2021-12-01 18:24 ` Florian Weimer
2021-12-02 9:53 ` Stefan Hajnoczi
2021-12-02 14:44 ` Peter Maydell
2021-12-02 14:50 ` Peter Maydell
2021-12-02 14:57 ` Florian Weimer
2021-12-03 6:24 ` Serge Guelton [this message]
2021-12-01 17:01 ` [RFC v2 2/4] util/async: replace __thread with QEMU TLS macros Stefan Hajnoczi
2021-12-01 17:01 ` [RFC v2 3/4] rcu: use coroutine " Stefan Hajnoczi
2021-12-01 17:01 ` [RFC v2 4/4] cpus: use coroutine TLS macros for iothread_locked Stefan Hajnoczi
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=20211203062414.GA1106@sguelton.remote.csb \
--to=sguelton@redhat.com \
--cc=berrange@redhat.com \
--cc=fam@euphon.net \
--cc=fweimer@redhat.com \
--cc=imp@bsdimp.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanha@redhat.com \
--cc=thuth@redhat.com \
/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 NNTP newsgroup(s).