From nobody Thu Dec 18 19:36:26 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1651675081; cv=none; d=zohomail.com; s=zohoarc; b=UMJp06kyonndmtRztZYb5zntfAebI2blIrxATxsxvV/kFYeMoGY+/vhy1aWgOucucWIXavF/Zqx0P8o9WVOyBossc+IdS16+HQI6HWyRngBg/XFVovsG2AZdWal10mNsBXw6PcwewlEBJaCbGb0z9xhFAStvwSJjE6s7z08B3RY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651675081; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=WLs/ZyOmTap6tSoe0X8sZsyHCHNLeD5jO0y/BaAN5Uo=; b=oBaIc5yX6rQiyuwtaeLfdYDSu84PZYPtHOI1Amiio5b97DNkYSgIX2H0i/gTpvYRNOr8kQNwFt/byQMlJKgpwb4IitRWJgIVMumSqxTbpxBx0a38KpXpj0jABUX0T65FbL1FNIHe+87xZ8pFpbr1XFlIIu5ks6ElylOXDzOuiH4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1651675081230502.0602001720828; Wed, 4 May 2022 07:38:01 -0700 (PDT) Received: from localhost ([::1]:57916 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nmG8e-0006t1-6N for importer@patchew.org; Wed, 04 May 2022 10:38:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44254) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nmFwn-0000v8-6I for qemu-devel@nongnu.org; Wed, 04 May 2022 10:25:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:45616) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nmFwk-0007I2-JQ for qemu-devel@nongnu.org; Wed, 04 May 2022 10:25:44 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-561-A-edhbaSMZ-g1fMyii9IOg-1; Wed, 04 May 2022 10:25:40 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A46A5801E80; Wed, 4 May 2022 14:25:39 +0000 (UTC) Received: from merkur.str.redhat.com (dhcp-192-180.str.redhat.com [10.33.192.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id 254D7402189; Wed, 4 May 2022 14:25:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651674341; 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=WLs/ZyOmTap6tSoe0X8sZsyHCHNLeD5jO0y/BaAN5Uo=; b=fqSjv00vhtDKWdrJXObWrwxK7Lf3Rjg9mjBuHssZ4UGL18GcZdzSnQ3HlCb6/4GJPrEtjo 2WMN56Wp3UBfv6dJwf4QOtklF/z266c0oMVj+61cy8BX1FXyCo0tYuJQn0mMXxvH5KSEKl kiTjviO602rkkvRsa6i7CfwYMAs3F7I= X-MC-Unique: A-edhbaSMZ-g1fMyii9IOg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 13/13] coroutine-win32: use QEMU_DEFINE_STATIC_CO_TLS() Date: Wed, 4 May 2022 16:25:22 +0200 Message-Id: <20220504142522.167506-14-kwolf@redhat.com> In-Reply-To: <20220504142522.167506-1-kwolf@redhat.com> References: <20220504142522.167506-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 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_LOW=-0.7, 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1651675082985100001 From: Stefan Hajnoczi 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 Message-Id: <20220307153853.602859-4-stefanha@redhat.com> Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Kevin Wolf --- 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 c196f956d2..7db2e8f8c8 100644 --- a/util/coroutine-win32.c +++ b/util/coroutine-win32.c @@ -24,6 +24,7 @@ =20 #include "qemu/osdep.h" #include "qemu/coroutine_int.h" +#include "qemu/coroutine-tls.h" =20 typedef struct { @@ -33,8 +34,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 @@ -51,7 +52,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); @@ -88,14 +89,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