All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: fweimer@redhat.com, Kevin Wolf <kwolf@redhat.com>,
	thuth@redhat.com, qemu-block@nongnu.org,
	Fam Zheng <fam@euphon.net>,
	sguelton@redhat.com
Subject: Re: [RFC 1/2] tls: add macros for coroutine-safe TLS variables
Date: Mon, 25 Oct 2021 10:19:04 -0700	[thread overview]
Message-ID: <fea06711-f4dd-9932-5b2d-06a408c7adf6@linaro.org> (raw)
In-Reply-To: <20211025140716.166971-2-stefanha@redhat.com>

On 10/25/21 7:07 AM, Stefan Hajnoczi 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.
...
>   include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++

Better as qemu/coroutine-tls.h, since it is needed for no other purpose.

> +#define QEMU_DEFINE_TLS(type, var) \
> +    __thread type qemu_tls_##var; \
> +    type get_##var(void) { return qemu_tls_##var; } \
> +    void set_##var(type v) { qemu_tls_##var = v; }

You might as well make the variable static, since it may only be referenced by these two 
functions.

> +#define QEMU_DEFINE_STATIC_TLS(type, var) \
> +    static __thread type qemu_tls_##var; \
> +    static __attribute__((noinline)) type get_##var(void); \
> +    static type get_##var(void) { return qemu_tls_##var; } \
> +    static __attribute__((noinline)) void set_##var(type v); \
> +    static void set_##var(type v) { qemu_tls_##var = v; }

You don't need separate function declarations; you can fold them together.

If would be nice to inline this when possible,

#if defined(__aarch64__)
#define QEMU_COROUTINE_TLS_ADDR(RET, VAR)                       \
     asm volatile("mrs %0, tpidr_el0\n\t"                        \
                  "add %0, %0, #:tprel_hi12:"#VAR", lsl #12\n\t" \
                  "add %0, %0, #:tprel_lo12_nc:"#VAR             \
                  : "=r"(RET))
#elif defined(__powerpc64__)
#define QEMU_COROUTINE_TLS_ADDR(RET, VAR)                       \
     asm volatile("addis %0,13,"#VAR"@tprel@ha\n\t"              \
                  "add   %0,%0,"#VAR"@tprel@l"                   \
                  : "=r"(RET))
#elif defined(__riscv)
#define QEMU_COROUTINE_TLS_ADDR(RET, VAR)                       \
     asm volatile("lui  %0,%%tprel_hi("#VAR")\n\t"               \
                  "add  %0,%0,%%tprel_add("#VAR")\n\t"           \
                  "addi %0,%0,%%tprel_lo("#VAR")"                \
                  : "=r"(RET))
#elif defined(__x86_64__)
#define QEMU_COROUTINE_TLS_ADDR(RET, VAR)                       \
     asm volatile("mov %%fs:"#VAR"@tpoff, %0" : "=r"(RET))
#endif

#ifdef QEMU_COROUTINE_TLS_ADDR
#define QEMU_COROUTINE_TLS_DECLARE(TYPE, VAR)                   \
     extern __thread TYPE co_tls_##VAR;                          \
     static inline TYPE get_##VAR(void)                          \
     { TYPE *p; QEMU_COROUTINE_TLS_ADDR(p, co_tls_##VAR); return *p; } \
     static inline void set_##VAR(TYPE v)                        \
     { TYPE *p; QEMU_COROUTINE_TLS_ADDR(p, co_tls_##VAR); *p = v; }
#else
     etc
#endif


r~


  parent reply	other threads:[~2021-10-25 17:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25 14:07 [RFC 0/2] tls: add macros for coroutine-safe TLS variables Stefan Hajnoczi
2021-10-25 14:07 ` [RFC 1/2] " Stefan Hajnoczi
2021-10-25 14:14   ` Daniel P. Berrangé
2021-10-26 13:36     ` Stefan Hajnoczi
2021-10-26 13:41     ` Stefan Hajnoczi
2021-10-26 14:10       ` Kevin Wolf
2021-10-26 16:26         ` Stefan Hajnoczi
2021-10-25 17:19   ` Richard Henderson [this message]
2021-10-26 13:30     ` Stefan Hajnoczi
2021-10-26 15:32       ` Richard Henderson
2021-10-26 16:27         ` Stefan Hajnoczi
2021-10-25 14:07 ` [RFC 2/2] util/async: replace __thread with QEMU TLS macros Stefan Hajnoczi
2021-10-25 14:20 ` [RFC 0/2] tls: add macros for coroutine-safe TLS variables Philippe Mathieu-Daudé
2021-10-25 16:16 ` Richard Henderson
2021-10-25 23:27   ` Warner Losh
2021-10-26 13:22     ` Stefan Hajnoczi
2021-10-26 15:10       ` Richard Henderson
2021-10-26 16:34         ` Stefan Hajnoczi
2021-10-26 17:10           ` Richard Henderson
2021-10-26 17:26             ` Thomas Huth
2021-10-26 18:03               ` Richard Henderson
2021-10-27 10:38             ` Kevin Wolf
2021-10-27 12:34             ` 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=fea06711-f4dd-9932-5b2d-06a408c7adf6@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=fam@euphon.net \
    --cc=fweimer@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sguelton@redhat.com \
    --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 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.