From nobody Thu Apr 3 11:43:04 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=ispras.ru ARC-Seal: i=1; a=rsa-sha256; t=1740413938; cv=none; d=zohomail.com; s=zohoarc; b=UYaR84CZTwv/8+FdjNc8nQ2edfcx+jGKN43HnxpsNSVc2K+zThIpbelBmTWdiKwCxTmPXTtQLm2I3aQlfVL1hFpKFEOapVf0DrROPOcB2NR0K5lBnTuKyCl8ikMovO+nhLsu7saRDQFHN+b7Fyka/eQzz+OVczDvsaNmTxZnRGE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740413938; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=jliidjZFzjNpSd+Y//G207eRjt/KjrtYVGHUO3SDMj4=; b=FccQWme2SXkga6U7EoqRGBu33K05Sgc4iq7f04eYR+cuWV70/WrrmZUy/xqCS/9Ar1jL5ptRvR0O83QoaDPv0iNOVBmDxRV9ehA7qyPCPTqDreTNszYDsv0u0UJFz2PJwW0NLcpbFXWuL2DcrXBMmXnejFkMlcFZJHUZwlXvysw= 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 1740413938263763.949456157724; Mon, 24 Feb 2025 08:18:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmb9E-000338-Uq; Mon, 24 Feb 2025 11:17:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tmb9B-00032j-KV for qemu-devel@nongnu.org; Mon, 24 Feb 2025 11:17:34 -0500 Received: from mail.ispras.ru ([83.149.199.84]) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tmb99-0004gj-G3 for qemu-devel@nongnu.org; Mon, 24 Feb 2025 11:17:33 -0500 Received: from Neilbrown.intra.ispras.ru (unknown [10.10.2.179]) by mail.ispras.ru (Postfix) with ESMTPSA id 9105C40B1E84; Mon, 24 Feb 2025 16:17:23 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 9105C40B1E84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1740413843; bh=jliidjZFzjNpSd+Y//G207eRjt/KjrtYVGHUO3SDMj4=; h=From:To:Cc:Subject:Date:From; b=ncVA7yFJQY1rBlLK5wncogINegp6p/KG9LuneKsRcb9pJxHv9vEeTYbWvlur1Ww93 69ojvZmuj/COasu6pBVVvK3Ssm+G+YPhYoDP/Lcndj26scDTAia/SbiuaAapm71cID kAaz38X1dYw1Iw1ui/BEI1DzPpkzazPPJ2tnuDDc= From: Vitalii Mordan To: Thomas Huth Cc: Vitalii Mordan , Stefan Hajnoczi , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Paolo Bonzini , qemu-devel@nongnu.org, sdl.qemu@linuxtesting.org, Vadim Mutilin , Alexey Khoroshilov Subject: [PATCH v2] Fix data race with the state Field of ThreadPoolElement Date: Mon, 24 Feb 2025 19:17:19 +0300 Message-Id: <20250224161719.3831357-1-mordan@ispras.ru> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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=83.149.199.84; envelope-from=mordan@ispras.ru; helo=mail.ispras.ru X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ispras.ru) X-ZM-MESSAGEID: 1740413943994019100 Content-Type: text/plain; charset="utf-8" TSAN reports a potential data race on the state field of ThreadPoolElement. This is fixed by using atomic access to the field. Fixes: d354c7eccf ("aio: add generic thread-pool facility") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2822 Signed-off-by: Vitalii Mordan --- v2: Addressed the Paolo Bonzini comments. util/thread-pool.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/util/thread-pool.c b/util/thread-pool.c index 27eb777e85..2ef3f1ff84 100644 --- a/util/thread-pool.c +++ b/util/thread-pool.c @@ -111,9 +111,8 @@ static void *worker_thread(void *opaque) ret =3D req->func(req->arg); =20 req->ret =3D ret; - /* Write ret before state. */ - smp_wmb(); - req->state =3D THREAD_DONE; + /* Atomically update state after setting ret. */ + qatomic_store_release(&req->state, THREAD_DONE); =20 qemu_bh_schedule(pool->completion_bh); qemu_mutex_lock(&pool->lock); @@ -180,7 +179,10 @@ static void thread_pool_completion_bh(void *opaque) =20 restart: QLIST_FOREACH_SAFE(elem, &pool->head, all, next) { - if (elem->state !=3D THREAD_DONE) { + /* Atomically access the state field to synchronize with the + * worker_thread and thread_pool_cancel. + */ + if (qatomic_load_acquire(&elem->state) !=3D THREAD_DONE) { continue; } =20 @@ -189,9 +191,6 @@ restart: QLIST_REMOVE(elem, all); =20 if (elem->common.cb) { - /* Read state before ret. */ - smp_rmb(); - /* Schedule ourselves in case elem->common.cb() calls aio_poll= () to * wait for another request that completed at the same time. */ @@ -223,12 +222,13 @@ static void thread_pool_cancel(BlockAIOCB *acb) trace_thread_pool_cancel(elem, elem->common.opaque); =20 QEMU_LOCK_GUARD(&pool->lock); - if (elem->state =3D=3D THREAD_QUEUED) { + if (qatomic_read(&elem->state) =3D=3D THREAD_QUEUED) { QTAILQ_REMOVE(&pool->request_list, elem, reqs); qemu_bh_schedule(pool->completion_bh); =20 - elem->state =3D THREAD_DONE; elem->ret =3D -ECANCELED; + /* Atomically update state after setting ret. */ + qatomic_store_release(&elem->state, THREAD_DONE); } =20 } --=20 2.34.1