From nobody Fri Apr 4 03:50:48 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=1739981629; cv=none; d=zohomail.com; s=zohoarc; b=OuHlrJdWc1RF1x6mXCcfIyZdo/NfRbyWSRSqY3y43RVhcaARK1WKvk3lN1rz+QYLjlN45IU7zWJa2OGoK48pKVjpHfV8qgW3PWnvr80KwSTbh9YxbsA6KRXhmrKpo813uxwWwWWhLRlq6gviauCuAuBLYhSev7hnuNBot+cdr7k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739981629; 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=uqIoSnKH103eFzVm2nfh1pMTVXziDfv19fsVMIGilXQ=; b=Ld+bIwlbv2dz9Sh4oKvjKwwxRWGvf2dMMV8sI2ycdr/bE4Fgnm7lQanmCmztGzwaoM7FbTCG5AYVzDvMX7eQLGD6zxHSNd5uOeNPPu3/sp0v0heIB83rw5FbwfiXmZ0hM6P0a8MDRbBHAvFmaPisANOuJpUj49PxD0SOGU4CblA= 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 1739981629069318.45506841622864; Wed, 19 Feb 2025 08:13:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tkmgq-0002Fg-40; Wed, 19 Feb 2025 11:12:48 -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 1tkmgo-0002FM-NK for qemu-devel@nongnu.org; Wed, 19 Feb 2025 11:12:46 -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 1tkmgl-0003fP-PE for qemu-devel@nongnu.org; Wed, 19 Feb 2025 11:12:46 -0500 Received: from Neilbrown.intra.ispras.ru (unknown [10.10.2.179]) by mail.ispras.ru (Postfix) with ESMTPSA id 5AD7D407851F; Wed, 19 Feb 2025 16:12:37 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 5AD7D407851F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1739981557; bh=uqIoSnKH103eFzVm2nfh1pMTVXziDfv19fsVMIGilXQ=; h=From:To:Cc:Subject:Date:From; b=UPAlDjJakiEEFgEXjYuy8ZtEbU08zS/PhO0hvheqlXaFsNfoAPg5RR0Wgs2gei0Am 3CnlPnBGGrvWytaU3uQbBMtk+fGAPriQH/6jFUbwdfKLJoLW2XcW6tFp7KHHSIG+ip JraDfmRRht0b2/+SmYTl4sMeQdWAcKBcWnVEBHSw= 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] Fix data race with the state Field of ThreadPoolElement Date: Wed, 19 Feb 2025 19:12:23 +0300 Message-Id: <20250219161223.3340431-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: 1739981631905019000 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 --- util/thread-pool.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/util/thread-pool.c b/util/thread-pool.c index 27eb777e85..6c5f4d085b 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,7 @@ static void thread_pool_completion_bh(void *opaque) =20 restart: QLIST_FOREACH_SAFE(elem, &pool->head, all, next) { - if (elem->state !=3D THREAD_DONE) { + if (qatomic_load_acquire(&elem->state) !=3D THREAD_DONE) { continue; } =20 @@ -223,12 +222,12 @@ 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_load_acquire(&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; + qatomic_store_release(&elem->state, THREAD_DONE); } =20 } @@ -251,8 +250,8 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func= , void *arg, req =3D qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque); req->func =3D func; req->arg =3D arg; - req->state =3D THREAD_QUEUED; req->pool =3D pool; + qatomic_store_release(&req->state, THREAD_QUEUED); =20 QLIST_INSERT_HEAD(&pool->head, req, all); =20 --=20 2.34.1