From nobody Sat May 18 21:00:25 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646668379924215.37399920300572; Mon, 7 Mar 2022 07:52:59 -0800 (PST) Received: from localhost ([::1]:58282 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRFfO-00022I-Jm for importer@patchew.org; Mon, 07 Mar 2022 10:52:58 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40560) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRFSB-0000v9-6K for qemu-devel@nongnu.org; Mon, 07 Mar 2022 10:39:19 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:28821) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRFS9-0004Cb-5T for qemu-devel@nongnu.org; Mon, 07 Mar 2022 10:39:18 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-609-0F83zgenPTSO_wkMvPDUfg-1; Mon, 07 Mar 2022 10:39:13 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9BDD11006AA8; Mon, 7 Mar 2022 15:39:12 +0000 (UTC) Received: from localhost (unknown [10.39.193.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E48C26343; Mon, 7 Mar 2022 15:39:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646667556; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w0lwfbgakbDPz2F1Wm8I8jfyO4wDR1n9q2xO5avVkiI=; b=WOYUYLL5rJeOYJcRRXERCIUFxa/LBJxlH7j3axem5lbFFlnnrR/wu4e1UeamjQ+JjX2mmo QGc6UO5MMWxlpTjNIR32fkHJcV8A62Y6LFrmf0em0RruzQA+oGeZYefceURjOpE481zO7E +IXRN0itcQb6B3kmGeu13eMfmPYFTAg= X-MC-Unique: 0F83zgenPTSO_wkMvPDUfg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 1/3] coroutine-ucontext: use QEMU_DEFINE_STATIC_CO_TLS() Date: Mon, 7 Mar 2022 15:38:51 +0000 Message-Id: <20220307153853.602859-2-stefanha@redhat.com> In-Reply-To: <20220307153853.602859-1-stefanha@redhat.com> References: <20220307153853.602859-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Stefan Weil , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646668381355100001 Content-Type: text/plain; charset="utf-8" Thread-Local Storage variables cannot be used directly from coroutine code because the compiler may optimize TLS variable accesses across qemu_coroutine_yield() calls. When the coroutine is re-entered from another thread the TLS variables from the old thread must no longer be used. Use QEMU_DEFINE_STATIC_CO_TLS() for the current and leader variables. Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- util/coroutine-ucontext.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/util/coroutine-ucontext.c b/util/coroutine-ucontext.c index 904b375192..127d5a13c8 100644 --- a/util/coroutine-ucontext.c +++ b/util/coroutine-ucontext.c @@ -25,6 +25,7 @@ #include "qemu/osdep.h" #include #include "qemu/coroutine_int.h" +#include "qemu/coroutine-tls.h" =20 #ifdef CONFIG_VALGRIND_H #include @@ -66,8 +67,8 @@ typedef struct { /** * Per-thread coroutine bookkeeping */ -static __thread CoroutineUContext leader; -static __thread Coroutine *current; +QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current); +QEMU_DEFINE_STATIC_CO_TLS(CoroutineUContext, leader); =20 /* * va_args to makecontext() must be type 'int', so passing @@ -97,14 +98,15 @@ static inline __attribute__((always_inline)) void finish_switch_fiber(void *fake_stack_save) { #ifdef CONFIG_ASAN + CoroutineUContext *leaderp =3D get_ptr_leader(); const void *bottom_old; size_t size_old; =20 __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_ol= d); =20 - if (!leader.stack) { - leader.stack =3D (void *)bottom_old; - leader.stack_size =3D size_old; + if (!leaderp->stack) { + leaderp->stack =3D (void *)bottom_old; + leaderp->stack_size =3D size_old; } #endif #ifdef CONFIG_TSAN @@ -161,8 +163,10 @@ static void coroutine_trampoline(int i0, int i1) =20 /* Initialize longjmp environment and switch back the caller */ if (!sigsetjmp(self->env, 0)) { - start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, leader.= stack, - leader.stack_size); + CoroutineUContext *leaderp =3D get_ptr_leader(); + + start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, + leaderp->stack, leaderp->stack_size); start_switch_fiber_tsan(&fake_stack_save, self, true); /* true=3Dc= aller */ siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); } @@ -297,7 +301,7 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, int ret; void *fake_stack_save =3D NULL; =20 - current =3D to_; + set_current(to_); =20 ret =3D sigsetjmp(from->env, 0); if (ret =3D=3D 0) { @@ -315,18 +319,24 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to= _, =20 Coroutine *qemu_coroutine_self(void) { - if (!current) { - current =3D &leader.base; + Coroutine *self =3D get_current(); + CoroutineUContext *leaderp =3D get_ptr_leader(); + + if (!self) { + self =3D &leaderp->base; + set_current(self); } #ifdef CONFIG_TSAN - if (!leader.tsan_co_fiber) { - leader.tsan_co_fiber =3D __tsan_get_current_fiber(); + if (!leaderp->tsan_co_fiber) { + leaderp->tsan_co_fiber =3D __tsan_get_current_fiber(); } #endif - return current; + return self; } =20 bool qemu_in_coroutine(void) { - return current && current->caller; + Coroutine *self =3D get_current(); + + return self && self->caller; } --=20 2.35.1 From nobody Sat May 18 21:00:25 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646668480628446.64054150905645; Mon, 7 Mar 2022 07:54:40 -0800 (PST) Received: from localhost ([::1]:36438 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRFh1-0006Js-Fq for importer@patchew.org; Mon, 07 Mar 2022 10:54:39 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40610) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRFSE-00019m-Uw for qemu-devel@nongnu.org; Mon, 07 Mar 2022 10:39:22 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:40777) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRFSD-0004DH-8Q for qemu-devel@nongnu.org; Mon, 07 Mar 2022 10:39:22 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-499-QpwTFtXnOZCQpDTy3M8RPw-1; Mon, 07 Mar 2022 10:39:19 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9B67E824FA8; Mon, 7 Mar 2022 15:39:18 +0000 (UTC) Received: from localhost (unknown [10.39.193.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3DBDB80009; Mon, 7 Mar 2022 15:39:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646667560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/xindar3VGj8mumCbzf67K+hTm+ax+lScp0gCxWqpxs=; b=Q6JfSb+lnGLHHN7De3Plpw2pZYyZeU8IAn0OPH5LI8tln4GZ6HSXXo5w8C/YIlbOt0VCHJ ypFa+4s6mlMri7l6XfanOKXneDQUplFrLz45Vs9umJePSlxdLYEYtsQw+3i/4Q41rVd2dm QXk12G5t2fK+4Z2CUANIU7Av+9EnSxE= X-MC-Unique: QpwTFtXnOZCQpDTy3M8RPw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 2/3] coroutine: use QEMU_DEFINE_STATIC_CO_TLS() Date: Mon, 7 Mar 2022 15:38:52 +0000 Message-Id: <20220307153853.602859-3-stefanha@redhat.com> In-Reply-To: <20220307153853.602859-1-stefanha@redhat.com> References: <20220307153853.602859-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Stefan Weil , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646668482073100001 Content-Type: text/plain; charset="utf-8" Thread-Local Storage variables cannot be used directly from coroutine code because the compiler may optimize TLS variable accesses across qemu_coroutine_yield() calls. When the coroutine is re-entered from another thread the TLS variables from the old thread must no longer be used. Use QEMU_DEFINE_STATIC_CO_TLS() for the current and leader variables. The alloc_pool QSLIST needs a typedef so the return value of get_ptr_alloc_pool() can be stored in a local variable. One example of why this code is necessary: a coroutine that yields before calling qemu_coroutine_create() to create another coroutine is affected by the TLS issue. Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- util/qemu-coroutine.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c index c03b2422ff..f3e8300c8d 100644 --- a/util/qemu-coroutine.c +++ b/util/qemu-coroutine.c @@ -18,6 +18,7 @@ #include "qemu/atomic.h" #include "qemu/coroutine.h" #include "qemu/coroutine_int.h" +#include "qemu/coroutine-tls.h" #include "block/aio.h" =20 /** Initial batch size is 64, and is increased on demand */ @@ -29,17 +30,20 @@ enum { static QSLIST_HEAD(, Coroutine) release_pool =3D QSLIST_HEAD_INITIALIZER(p= ool); static unsigned int pool_batch_size =3D POOL_INITIAL_BATCH_SIZE; static unsigned int release_pool_size; -static __thread QSLIST_HEAD(, Coroutine) alloc_pool =3D QSLIST_HEAD_INITIA= LIZER(pool); -static __thread unsigned int alloc_pool_size; -static __thread Notifier coroutine_pool_cleanup_notifier; + +typedef QSLIST_HEAD(, Coroutine) CoroutineQSList; +QEMU_DEFINE_STATIC_CO_TLS(CoroutineQSList, alloc_pool); +QEMU_DEFINE_STATIC_CO_TLS(unsigned int, alloc_pool_size); +QEMU_DEFINE_STATIC_CO_TLS(Notifier, coroutine_pool_cleanup_notifier); =20 static void coroutine_pool_cleanup(Notifier *n, void *value) { Coroutine *co; Coroutine *tmp; + CoroutineQSList *alloc_pool =3D get_ptr_alloc_pool(); =20 - QSLIST_FOREACH_SAFE(co, &alloc_pool, pool_next, tmp) { - QSLIST_REMOVE_HEAD(&alloc_pool, pool_next); + QSLIST_FOREACH_SAFE(co, alloc_pool, pool_next, tmp) { + QSLIST_REMOVE_HEAD(alloc_pool, pool_next); qemu_coroutine_delete(co); } } @@ -49,27 +53,30 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry,= void *opaque) Coroutine *co =3D NULL; =20 if (CONFIG_COROUTINE_POOL) { - co =3D QSLIST_FIRST(&alloc_pool); + CoroutineQSList *alloc_pool =3D get_ptr_alloc_pool(); + + co =3D QSLIST_FIRST(alloc_pool); if (!co) { if (release_pool_size > qatomic_read(&pool_batch_size)) { /* Slow path; a good place to register the destructor, too= . */ - if (!coroutine_pool_cleanup_notifier.notify) { - coroutine_pool_cleanup_notifier.notify =3D coroutine_p= ool_cleanup; - qemu_thread_atexit_add(&coroutine_pool_cleanup_notifie= r); + Notifier *notifier =3D get_ptr_coroutine_pool_cleanup_noti= fier(); + if (!notifier->notify) { + notifier->notify =3D coroutine_pool_cleanup; + qemu_thread_atexit_add(notifier); } =20 /* This is not exact; there could be a little skew between * release_pool_size and the actual size of release_pool. = But * it is just a heuristic, it does not need to be perfect. */ - alloc_pool_size =3D qatomic_xchg(&release_pool_size, 0); - QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool); - co =3D QSLIST_FIRST(&alloc_pool); + set_alloc_pool_size(qatomic_xchg(&release_pool_size, 0)); + QSLIST_MOVE_ATOMIC(alloc_pool, &release_pool); + co =3D QSLIST_FIRST(alloc_pool); } } if (co) { - QSLIST_REMOVE_HEAD(&alloc_pool, pool_next); - alloc_pool_size--; + QSLIST_REMOVE_HEAD(alloc_pool, pool_next); + set_alloc_pool_size(get_alloc_pool_size() - 1); } } =20 @@ -93,9 +100,9 @@ static void coroutine_delete(Coroutine *co) qatomic_inc(&release_pool_size); return; } - if (alloc_pool_size < qatomic_read(&pool_batch_size)) { - QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next); - alloc_pool_size++; + if (get_alloc_pool_size() < qatomic_read(&pool_batch_size)) { + QSLIST_INSERT_HEAD(get_ptr_alloc_pool(), co, pool_next); + set_alloc_pool_size(get_alloc_pool_size() + 1); return; } } --=20 2.35.1 From nobody Sat May 18 21:00:25 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 164666834336713.362531046344884; Mon, 7 Mar 2022 07:52:23 -0800 (PST) Received: from localhost ([::1]:57078 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRFeo-0001FF-Bf for importer@patchew.org; Mon, 07 Mar 2022 10:52:22 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40704) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRFSW-0001se-FU for qemu-devel@nongnu.org; Mon, 07 Mar 2022 10:39:40 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:23091) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRFSU-0004Fj-UI for qemu-devel@nongnu.org; Mon, 07 Mar 2022 10:39:40 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-645-Zucy5zwaOtWELFOPLrBDCQ-1; Mon, 07 Mar 2022 10:39:35 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EDF9251DC; Mon, 7 Mar 2022 15:39:33 +0000 (UTC) Received: from localhost (unknown [10.39.193.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E8A77DE58; Mon, 7 Mar 2022 15:39:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646667578; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WtPyu4SE08yqpJyQZ1vSYghMG9hrA1SFpeRBeHQXJcE=; b=DgYY5uN3JQxuyPXMAogt6w9rVTyUkvvl8Ljx89+49Ol6jcFaUroFNW0tatuh/XPI7LlEFy PZYCDKD4emfJ1KcyWuRuTrH2byUm7nmIwJhv9b5KV2HsMex3tocLfNsD0sbkMcTkMj11oh TkPHwR6QUqasI7046xDOL34FL0Q7ec8= X-MC-Unique: Zucy5zwaOtWELFOPLrBDCQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 3/3] coroutine-win32: use QEMU_DEFINE_STATIC_CO_TLS() Date: Mon, 7 Mar 2022 15:38:53 +0000 Message-Id: <20220307153853.602859-4-stefanha@redhat.com> In-Reply-To: <20220307153853.602859-1-stefanha@redhat.com> References: <20220307153853.602859-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Stefan Weil , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646668345064100001 Content-Type: text/plain; charset="utf-8" Thread-Local Storage variables cannot be used directly from coroutine code because the compiler may optimize TLS variable accesses across qemu_coroutine_yield() calls. When the coroutine is re-entered from another thread the TLS variables from the old thread must no longer be used. Use QEMU_DEFINE_STATIC_CO_TLS() for the current and leader variables. I think coroutine-win32.c could get away with __thread because the variables are only used in situations where either the stale value is correct (current) or outside coroutine context (loading leader when current is NULL). Due to the difficulty of being sure that this is really safe in all scenarios it seems worth converting it anyway. Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- util/coroutine-win32.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/util/coroutine-win32.c b/util/coroutine-win32.c index de6bd4fd3e..c02a62c896 100644 --- a/util/coroutine-win32.c +++ b/util/coroutine-win32.c @@ -25,6 +25,7 @@ #include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/coroutine_int.h" +#include "qemu/coroutine-tls.h" =20 typedef struct { @@ -34,8 +35,8 @@ typedef struct CoroutineAction action; } CoroutineWin32; =20 -static __thread CoroutineWin32 leader; -static __thread Coroutine *current; +QEMU_DEFINE_STATIC_CO_TLS(CoroutineWin32, leader); +QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current); =20 /* This function is marked noinline to prevent GCC from inlining it * into coroutine_trampoline(). If we allow it to do that then it @@ -52,7 +53,7 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, CoroutineWin32 *from =3D DO_UPCAST(CoroutineWin32, base, from_); CoroutineWin32 *to =3D DO_UPCAST(CoroutineWin32, base, to_); =20 - current =3D to_; + set_current(to_); =20 to->action =3D action; SwitchToFiber(to->fiber); @@ -89,14 +90,21 @@ void qemu_coroutine_delete(Coroutine *co_) =20 Coroutine *qemu_coroutine_self(void) { + Coroutine *current =3D get_current(); + if (!current) { - current =3D &leader.base; - leader.fiber =3D ConvertThreadToFiber(NULL); + CoroutineWin32 *leader =3D get_ptr_leader(); + + current =3D &leader->base; + set_current(current); + leader->fiber =3D ConvertThreadToFiber(NULL); } return current; } =20 bool qemu_in_coroutine(void) { + Coroutine *current =3D get_current(); + return current && current->caller; } --=20 2.35.1