Skip to contentSkip to conversation

tls: add macros for coroutine-safe TLS variables

RFC Superseded Stefan Hajnoczi 7 participants 23 messages Oct 25, 2021 – Oct 27, 2021

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover tls: add macros for coroutine-safe TLS variables

This is a preview of how we can solve the coroutines TLS problem. Coroutines re-entered from another thread sometimes see stale TLS values. This happens because compilers may cache values across yield points, so a value from the previous thread will be used when the coroutine is re-entered in another thread.

Serge Guelton developed this technique, see the first patch for details. I'm submitting it for discussion before I go ahead with a full conversion of the source tree.

Todo:
- Convert all uses of __thread
- Extend checkpatch.pl to reject code that uses __thread

Stefan Hajnoczi (2):
tls: add macros for coroutine-safe TLS variables util/async: replace __thread with QEMU TLS macros

 MAINTAINERS        |   1 +
 include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++
 util/async.c       |  12 ++--
 3 files changed, 150 insertions(+), 5 deletions(-)
 create mode 100644 include/qemu/tls.h
Signature · 1 line
2.31.1
#1 lore raw

RFC 1/2 tls: add macros for coroutine-safe TLS variables

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.

These macros must be used instead of __thread from now on to prevent coroutine TLS bugs.

---
 MAINTAINERS        |   1 +
 include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+)
 create mode 100644 include/qemu/tls.h
MAINTAINERS +1 -0
index 894dc43105..cf225b7029 100644
@@ -2869,6 +2869,7 @@ M: Kevin Wolf <[email protected]>
 S: Maintained
 F: util/*coroutine*
 F: include/qemu/coroutine*
+F: include/qemu/tls.h
 F: tests/unit/test-coroutine.c
 
 Buffers
include/qemu/tls.h new file +142 -0

142 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw
Quoted 10 lines · Stefan Hajnoczi

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.

These macros must be used instead of __thread from now on to prevent coroutine TLS bugs.

Does this apply to all __thread usage in the QEMU process that can be used from coroutine context, or just certain __thread usage ?

Mostly I'm wondering if this is going to have implications on external libraries we use. eg if block layer is using librbd.so APIs, is librbd.sp safe to use __thread directly in any way it desires ?

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 12 lines · Daniel P. Berrangé
Quoted 10 lines · Stefan Hajnoczi

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.

These macros must be used instead of __thread from now on to prevent coroutine TLS bugs.

Quoted 6 lines

Does this apply to all __thread usage in the QEMU process that can be used from coroutine context, or just certain __thread usage ?

Mostly I'm wondering if this is going to have implications on external libraries we use. eg if block layer is using librbd.so APIs, is librbd.sp safe to use __thread directly in any way it desires ?

There is a theoretical problem but I'm not sure if it will occur in practice:

If a __thread variable is in an extern function then there's little risk of the value being cached. The function executes and returns back to QEMU, never yielding.

The only case I can think of is when the library accesses a __thread variable, invokes a callback into QEMU, and that callback yields. If the coroutine is re-entered from another thread and returns back to the library, then we have a problem. This seems like a very rare case though.

It gets trickier if the library has the __thread variable in a header file, because then the compiler may optimize it into a QEMU coroutine function and cache its value.

Stefan

#4 lore raw
In reply to Daniel P. Berrangé

Actually, nevermind what I said about the callback scenario. I don't think that is a problem because the compiler cannot assume the __thread variable remains unchanged across the callback. Therefore it cannot safely cache the value.

So I think only the header file scenario is a problem.

Stefan

#5 lore raw

Am 26.10.2021 um 15:41 hat Stefan Hajnoczi geschrieben:

Quoted 4 lines

Actually, nevermind what I said about the callback scenario. I don't think that is a problem because the compiler cannot assume the __thread variable remains unchanged across the callback. Therefore it cannot safely cache the value.

And additionally, I think callbacks are never coroutine_fn (especially not if they come from an external library), so they must not yield anyway.

Quoted 1 line

So I think only the header file scenario is a problem.

The mere existence of a __thread variable in the header file isn't a problem either, but if QEMU accesses it, we would have to implement wrappers similar to what you're proposing for QEMU's thread local variables.

Kevin

#6 lore raw
Quoted 9 lines · Kevin Wolf

Am 26.10.2021 um 15:41 hat Stefan Hajnoczi geschrieben:

Quoted 4 lines

Actually, nevermind what I said about the callback scenario. I don't think that is a problem because the compiler cannot assume the __thread variable remains unchanged across the callback. Therefore it cannot safely cache the value.

And additionally, I think callbacks are never coroutine_fn (especially not if they come from an external library), so they must not yield anyway.

There's a tiny chance that the third-party library was called from coroutine context and the callback invokes a non-coroutine_fn that uses qemu_in_coroutine() to dynamically decide it's possible to yield. But it seems very unlikely.

Quoted 6 lines
Quoted 1 line

So I think only the header file scenario is a problem.

The mere existence of a __thread variable in the header file isn't a problem either, but if QEMU accesses it, we would have to implement wrappers similar to what you're proposing for QEMU's thread local variables.

There could be static inline functions that access it in the header file. If QEMU calls those functions then the compiler can optimize that.

Stefan

#7 lore raw
In reply to Stefan Hajnoczi
Quoted 3 lines · Stefan Hajnoczi

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.

...

Quoted 1 line
  include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++

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

include/qemu/tls.h line 108

Quoted 4 lines
include/qemu/tls.h new file +4 -0
+#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.

include/qemu/tls.h line 140

Quoted 6 lines
include/qemu/tls.h new file +6 -0
+#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~

#8 lore raw
Quoted 9 lines · Richard Henderson
Quoted 3 lines · Stefan Hajnoczi

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.

...

Quoted 1 line
  include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++

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

Quoted 7 lines

include/qemu/tls.h line 108

Quoted 4 lines
include/qemu/tls.h new file +4 -0
+#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.

Oops, that's a bug. It should be declared extern. QEMU_DEFINE_TLS() is meant for use in header files.

Quoted 41 lines

include/qemu/tls.h line 140

Quoted 7 lines
include/qemu/tls.h new file +6 -0
+#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; }
Quoted 3 lines

#else
etc
#endif

Nice. That makes me wonder if it's possible to write a portable version:

  static inline TYPE get_##VAR(void) \
  { volatile TYPE *p = &co_tls_##VAR; return *p; }

But the trouble is we need &co_tls_##VAR to be "volatile" and I don't think there is a way to express that?

Stefan

#9 lore raw
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · Richard Henderson
Quoted 3 lines · Stefan Hajnoczi

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.

...

Quoted 1 line
   include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++

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

Quoted 4 lines
include/qemu/tls.h new file +4 -0
+#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; }
Quoted 5 lines
Quoted 2 lines

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

Oops, that's a bug. It should be declared extern. QEMU_DEFINE_TLS() is meant for use in header files.

No, QEMU_DECLARE_TLS was for use in header files, and it of course does not globally declare the tls variable at all. Only the definition here requires the tls variable.

Quoted 7 lines

Nice. That makes me wonder if it's possible to write a portable version:

   static inline TYPE get_##VAR(void) \
   { volatile TYPE *p = &co_tls_##VAR; return *p; }

But the trouble is we need &co_tls_##VAR to be "volatile" and I don't think there is a way to express that?

No, there's not.

Anyway, with those four hosts we get coverage of almost all users. I'll note that both arm32 and s390x use the constant pool in computing these tls addresses, so they'll need to keep using your functional version. So we'll still have testing of that path as well.

r~

#10 lore raw
Quoted 19 lines · Richard Henderson
Quoted 18 lines · Stefan Hajnoczi
Quoted 16 lines · Richard Henderson
Quoted 3 lines · Stefan Hajnoczi

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.

...

Quoted 1 line
   include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++

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

include/qemu/tls.h line 108

Quoted 4 lines
include/qemu/tls.h new file +4 -0
+#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.

Quoted 6 lines
Quoted 2 lines

Oops, that's a bug. It should be declared extern. QEMU_DEFINE_TLS() is meant for use in header files.

No, QEMU_DECLARE_TLS was for use in header files, and it of course does not globally declare the tls variable at all. Only the definition here requires the tls variable.

You are right, thanks for pointing this out.

Quoted 9 lines
Quoted 8 lines

Nice. That makes me wonder if it's possible to write a portable version:

   static inline TYPE get_##VAR(void) \
   { volatile TYPE *p = &co_tls_##VAR; return *p; }

But the trouble is we need &co_tls_##VAR to be "volatile" and I don't think there is a way to express that?

Quoted 6 lines

No, there's not.

Anyway, with those four hosts we get coverage of almost all users. I'll note that both arm32 and s390x use the constant pool in computing these tls addresses, so they'll need to keep using your functional version. So we'll still have testing of that path as well.

Okay, thanks!

Stefan

#11 lore raw

RFC 2/2 util/async: replace __thread with QEMU TLS macros

In reply to Stefan Hajnoczi

QEMU TLS macros must be used to make TLS variables safe with coroutines.

---
 util/async.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
util/async.c +7 -5
index 6f6717a34b..9033f22a20 100644
@@ -32,6 +32,7 @@
 #include "qemu/rcu_queue.h"
 #include "block/raw-aio.h"
 #include "qemu/coroutine_int.h"
+#include "qemu/tls.h"
 #include "trace.h"
 
 /***********************************************************/
@@ -669,12 +670,13 @@ void aio_context_release(AioContext *ctx)
     qemu_rec_mutex_unlock(&ctx->lock);
 }
 
-static __thread AioContext *my_aiocontext;
+QEMU_DEFINE_STATIC_TLS(AioContext *, my_aiocontext)
 
 AioContext *qemu_get_current_aio_context(void)
 {
-    if (my_aiocontext) {
-        return my_aiocontext;
+    AioContext *ctx = get_my_aiocontext();
+    if (ctx) {
+        return ctx;
     }
     if (qemu_mutex_iothread_locked()) {
         /* Possibly in a vCPU thread.  */
@@ -685,6 +687,6 @@ AioContext *qemu_get_current_aio_context(void)
 
 void qemu_set_current_aio_context(AioContext *ctx)
 {
-    assert(!my_aiocontext);
-    my_aiocontext = ctx;
+    assert(!get_my_aiocontext());
+    set_my_aiocontext(ctx);
 }
Signature · 1 line
2.31.1
#12 lore raw
In reply to Stefan Hajnoczi

+Richard/Peter

Quoted 9 lines · Stefan Hajnoczi

This is a preview of how we can solve the coroutines TLS problem. Coroutines re-entered from another thread sometimes see stale TLS values. This happens because compilers may cache values across yield points, so a value from the previous thread will be used when the coroutine is re-entered in another thread.

Serge Guelton developed this technique, see the first patch for details. I'm submitting it for discussion before I go ahead with a full conversion of the source tree.

Beside the point Daniel raised (shared libs) this sensible approach LGTM.

Quoted 3 lines

Todo:
- Convert all uses of __thread

$ git grep __thread | wc -l
55

:/

Quoted 12 lines

- Extend checkpatch.pl to reject code that uses __thread

Stefan Hajnoczi (2):
tls: add macros for coroutine-safe TLS variables util/async: replace __thread with QEMU TLS macros

 MAINTAINERS        |   1 +
 include/qemu/tls.h | 142 +++++++++++++++++++++++++++++++++++++++++++++
 util/async.c       |  12 ++--
 3 files changed, 150 insertions(+), 5 deletions(-)
 create mode 100644 include/qemu/tls.h
#13 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi

This is a preview of how we can solve the coroutines TLS problem. Coroutines re-entered from another thread sometimes see stale TLS values. This happens because compilers may cache values across yield points, so a value from the previous thread will be used when the coroutine is re-entered in another thread.

I'm not thrilled by this, but I guess it does work.

It could be worthwhile to add some inline asm instead for specific hosts -- one instruction instead of an out-of-line call.

Quoted 7 lines

Serge Guelton developed this technique, see the first patch for details. I'm submitting it for discussion before I go ahead with a full conversion of the source tree.

Todo:
- Convert all uses of __thread
- Extend checkpatch.pl to reject code that uses __thread

Absolutely not. *Perhaps* one or two tls variables which are accessible by coroutines, but there are plenty that have absolutely no relation. Especially everything related to user-only execution.

r~

#14 lore raw
Quoted 27 lines · Richard Henderson
Quoted 1 line · Stefan Hajnoczi

This is a preview of how we can solve the coroutines TLS problem.

Coroutines

Quoted 1 line

re-entered from another thread sometimes see stale TLS values. This

happens

Quoted 1 line

because compilers may cache values across yield points, so a value from

the

Quoted 2 lines

previous thread will be used when the coroutine is re-entered in another thread.

I'm not thrilled by this, but I guess it does work.

It could be worthwhile to add some inline asm instead for specific hosts -- one
instruction instead of an out-of-line call.

Quoted 1 line

Serge Guelton developed this technique, see the first patch for details.

I'm

Quoted 1 line

submitting it for discussion before I go ahead with a full conversion of

the

Quoted 5 lines

source tree.

Todo:
- Convert all uses of __thread
- Extend checkpatch.pl to reject code that uses __thread

Quoted 6 lines

Absolutely not. *Perhaps* one or two tls variables which are accessible by coroutines,
but there are plenty that have absolutely no relation. Especially everything related to
user-only execution.

I had the same worry. I'd also worry that the hoops that are jumped through for
coroutines would somehow conflict with the low-level user-only execution environment. I mean, it should be fine, but I know I'd be cranky if I traced obscure regressions to being forced to use this construct...

Warner

Quoted 3 lines

r~

#15 lore raw
Quoted 37 lines · Warner Losh
Quoted 27 lines · Richard Henderson
Quoted 1 line · Stefan Hajnoczi

This is a preview of how we can solve the coroutines TLS problem.

Coroutines

Quoted 1 line

re-entered from another thread sometimes see stale TLS values. This

happens

Quoted 1 line

because compilers may cache values across yield points, so a value from

the

Quoted 2 lines

previous thread will be used when the coroutine is re-entered in another thread.

I'm not thrilled by this, but I guess it does work.

It could be worthwhile to add some inline asm instead for specific hosts -- one
instruction instead of an out-of-line call.

Quoted 1 line

Serge Guelton developed this technique, see the first patch for details.

I'm

Quoted 1 line

submitting it for discussion before I go ahead with a full conversion of

the

Quoted 5 lines

source tree.

Todo:
- Convert all uses of __thread
- Extend checkpatch.pl to reject code that uses __thread

Quoted 6 lines

Absolutely not. *Perhaps* one or two tls variables which are accessible by coroutines,
but there are plenty that have absolutely no relation. Especially everything related to
user-only execution.

Quoted 5 lines

I had the same worry. I'd also worry that the hoops that are jumped through for
coroutines would somehow conflict with the low-level user-only execution environment. I mean, it should be fine, but I know I'd be cranky if I traced obscure regressions to being forced to use this construct...

I have the opposite worry:

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

That's why I think all __thread variables should be converted.

Stefan

#16 lore raw
Quoted 4 lines · Stefan Hajnoczi

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

Co-routines are never used in user-only builds.

r~

#17 lore raw
Quoted 7 lines · Richard Henderson
Quoted 4 lines · Stefan Hajnoczi

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

Co-routines are never used in user-only builds.

If developers have the choice of using __thread then bugs can slip through.

Your assembly get_addr() approach reduces the performance overhead of TLS getters/setters.

Are you concerned about performance, the awkwardness of calling getters/setters, or something else for qemu-user?

Stefan

#18 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 7 lines · Richard Henderson
Quoted 4 lines · Stefan Hajnoczi

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

Co-routines are never used in user-only builds.

If developers have the choice of using __thread then bugs can slip through.

Huh? How. No, really.

Quoted 2 lines

Are you concerned about performance, the awkwardness of calling getters/setters, or something else for qemu-user?

Awkwardness first, performance second.

I'll also note that coroutines never run on vcpu threads, only io threads. So I'll resist any use of these interfaces in TCG as well.

r~

#19 lore raw
Quoted 13 lines · Richard Henderson
Quoted 11 lines · Stefan Hajnoczi
Quoted 7 lines · Richard Henderson
Quoted 4 lines · Stefan Hajnoczi

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

Co-routines are never used in user-only builds.

If developers have the choice of using __thread then bugs can slip through.

Quoted 8 lines

Huh?  How.  No, really.

Quoted 2 lines

Are you concerned about performance, the awkwardness of calling getters/setters, or something else for qemu-user?

Awkwardness first, performance second.

I'll also note that coroutines never run on vcpu threads, only io threads. So I'll resist any use of these interfaces in TCG as well.

Would it maybe make sense to tweak check_patch.pl to forbid __thread in certain folders only, e.g. block/ and util/ (i.e. where we know that there might be code that the iothreads are using)?

Thomas

#20 lore raw
Quoted 1 line · Thomas Huth

Would it maybe make sense to tweak check_patch.pl to forbid __thread in certain folders only, e.g. block/ and util/ (i.e. where we know that there might be code that the iothreads are using)?

This sounds plausible; hw/ too, perhaps.

r~

#21 lore raw
In reply to Richard Henderson

Am 26.10.2021 um 19:10 hat Richard Henderson geschrieben:

Quoted 15 lines · Stefan Hajnoczi
Quoted 11 lines · Richard Henderson
Quoted 7 lines · Stefan Hajnoczi
Quoted 4 lines

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

Co-routines are never used in user-only builds.

If developers have the choice of using __thread then bugs can slip through.

Huh? How. No, really.

Quoted 7 lines
Quoted 2 lines

Are you concerned about performance, the awkwardness of calling getters/setters, or something else for qemu-user?

Awkwardness first, performance second.

I'll also note that coroutines never run on vcpu threads, only io threads. So I'll resist any use of these interfaces in TCG as well.

This is wrong. Device emulation is called from vcpu threads, and it calls into code using coroutines. Using dedicated iothreads is possible for some devices, but not the default.

In fact, this is probably where the most visible case of the bug comes
from: A coroutine is created in the vcpu thread (while holding the BQL) and after yielding first, it is subsequently reentered from the main thread. This is exactly the same pattern as you often get with callbacks, where the request is made in the vcpu thread and the callback is run in the main thread.

Kevin

#22 lore raw
In reply to Richard Henderson
Quoted 8 lines · Richard Henderson
Quoted 7 lines · Stefan Hajnoczi
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Stefan Hajnoczi

If "safe" TLS variables are opt-in then we'll likely have obscure bugs when code changes to access a TLS variable that was previously never accessed from a coroutine. There is no compiler error and no way to detect this. When it happens debugging it is painful.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Co-routines are never used in user-only builds.

If developers have the choice of using __thread then bugs can slip through.

Huh? How. No, really.

If there is no checkpatch.pl error then more instances of __thread will slip in. Not everyone in the QEMU community will be aware of this issue, so it's likely that code with __thread will get merged.

Subsystems that use coroutines today include block, 9p, mpqemu, io channels, migration, colo, and monitor commands.

I understand that qemu-user is particularly unlikely to use coroutines. Thomas' suggestion sounds good to me. Let's allow __thread only in subsystems where it's safe.

Stefan