[PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked

Stefan Hajnoczi posted 4 patches 3 years, 11 months ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Fam Zheng <fam@euphon.net>, Richard Henderson <richard.henderson@linaro.org>
There is a newer version of this series
[PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked
Posted by Stefan Hajnoczi 3 years, 11 months ago
qemu_mutex_iothread_locked() may be used from coroutines. Standard
__thread variables cannot be used by coroutines. Use the coroutine TLS
macros instead.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 softmmu/cpus.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/softmmu/cpus.c b/softmmu/cpus.c
index 035395ae13..005a5c31ef 100644
--- a/softmmu/cpus.c
+++ b/softmmu/cpus.c
@@ -473,11 +473,11 @@ bool qemu_in_vcpu_thread(void)
     return current_cpu && qemu_cpu_is_self(current_cpu);
 }
 
-static __thread bool iothread_locked = false;
+QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
 
 bool qemu_mutex_iothread_locked(void)
 {
-    return iothread_locked;
+    return get_iothread_locked();
 }
 
 /*
@@ -490,13 +490,13 @@ void qemu_mutex_lock_iothread_impl(const char *file, int line)
 
     g_assert(!qemu_mutex_iothread_locked());
     bql_lock(&qemu_global_mutex, file, line);
-    iothread_locked = true;
+    set_iothread_locked(true);
 }
 
 void qemu_mutex_unlock_iothread(void)
 {
     g_assert(qemu_mutex_iothread_locked());
-    iothread_locked = false;
+    set_iothread_locked(false);
     qemu_mutex_unlock(&qemu_global_mutex);
 }
 
-- 
2.34.1


Re: [PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked
Posted by Philippe Mathieu-Daudé 3 years, 11 months ago
On 21/2/22 15:29, Stefan Hajnoczi wrote:
> qemu_mutex_iothread_locked() may be used from coroutines. Standard
> __thread variables cannot be used by coroutines. Use the coroutine TLS
> macros instead.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   softmmu/cpus.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Re: [PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked
Posted by Philippe Mathieu-Daudé 3 years, 11 months ago
On 21/2/22 15:29, Stefan Hajnoczi wrote:
> qemu_mutex_iothread_locked() may be used from coroutines. Standard
> __thread variables cannot be used by coroutines. Use the coroutine TLS
> macros instead.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   softmmu/cpus.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/softmmu/cpus.c b/softmmu/cpus.c
> index 035395ae13..005a5c31ef 100644
> --- a/softmmu/cpus.c
> +++ b/softmmu/cpus.c
> @@ -473,11 +473,11 @@ bool qemu_in_vcpu_thread(void)
>       return current_cpu && qemu_cpu_is_self(current_cpu);
>   }
>   
> -static __thread bool iothread_locked = false;
> +QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)

While "qemu/coroutine-tls.h" is indirectly included by "rcu.h",
please include it explicitly.

Re: [PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked
Posted by Stefan Hajnoczi 3 years, 11 months ago
On Mon, Feb 21, 2022 at 04:09:06PM +0100, Philippe Mathieu-Daudé wrote:
> On 21/2/22 15:29, Stefan Hajnoczi wrote:
> > qemu_mutex_iothread_locked() may be used from coroutines. Standard
> > __thread variables cannot be used by coroutines. Use the coroutine TLS
> > macros instead.
> > 
> > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> > ---
> >   softmmu/cpus.c | 8 ++++----
> >   1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/softmmu/cpus.c b/softmmu/cpus.c
> > index 035395ae13..005a5c31ef 100644
> > --- a/softmmu/cpus.c
> > +++ b/softmmu/cpus.c
> > @@ -473,11 +473,11 @@ bool qemu_in_vcpu_thread(void)
> >       return current_cpu && qemu_cpu_is_self(current_cpu);
> >   }
> > -static __thread bool iothread_locked = false;
> > +QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
> 
> While "qemu/coroutine-tls.h" is indirectly included by "rcu.h",
> please include it explicitly.

Thanks, will fix.

Stefan
Re: [PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked
Posted by Paolo Bonzini 3 years, 11 months ago
On 2/21/22 15:29, Stefan Hajnoczi wrote:
> -static __thread bool iothread_locked = false;
> +QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
>   
>   bool qemu_mutex_iothread_locked(void)
>   {
> -    return iothread_locked;
> +    return get_iothread_locked();
>   }
>   

Can we rename either the variable or the function, and avoid the wrapper 
altogether?

Paolo

Re: [PATCH v4 4/4] cpus: use coroutine TLS macros for iothread_locked
Posted by Peter Maydell 3 years, 11 months ago
On Wed, 23 Feb 2022 at 09:45, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> On 2/21/22 15:29, Stefan Hajnoczi wrote:
> > -static __thread bool iothread_locked = false;
> > +QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
> >
> >   bool qemu_mutex_iothread_locked(void)
> >   {
> > -    return iothread_locked;
> > +    return get_iothread_locked();
> >   }
> >
>
> Can we rename either the variable or the function, and avoid the wrapper
> altogether?

I think it's useful to distinguish the API for the rest of QEMU
(a function) from the implementation used internally (previously
a thread-local static, now a similar thing with wrappers.)

-- PMM