From nobody Thu Apr 25 16:33:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 1552594154626630.1057647653128; Thu, 14 Mar 2019 13:09:14 -0700 (PDT) Received: from localhost ([127.0.0.1]:44200 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4Wf7-0002Vu-Nb for importer@patchew.org; Thu, 14 Mar 2019 16:09:09 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33212) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4WT7-0000dr-KW for qemu-devel@nongnu.org; Thu, 14 Mar 2019 15:56:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4WEU-0004DR-5K for qemu-devel@nongnu.org; Thu, 14 Mar 2019 15:41:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54532) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4WET-0004Cc-Tr for qemu-devel@nongnu.org; Thu, 14 Mar 2019 15:41:38 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2C7D23088A85; Thu, 14 Mar 2019 18:25:37 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-69.ams2.redhat.com [10.36.112.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47B571001DD4; Thu, 14 Mar 2019 18:25:35 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 14 Mar 2019 19:25:35 +0100 Message-Id: <20190314182535.18332-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Thu, 14 Mar 2019 18:25:37 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] test-thread-pool: be more reliable X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" There is a rare race between the atomic_cmpxchg and bdrv_aio_cancel/bdrv_aio_cancel_async invocations. Detect it, the only sensible we can do about it is to exit long_cb immediately. Signed-off-by: Paolo Bonzini --- tests/test-thread-pool.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/tests/test-thread-pool.c b/tests/test-thread-pool.c index 9cdccb3a47..0b675923f6 100644 --- a/tests/test-thread-pool.c +++ b/tests/test-thread-pool.c @@ -27,9 +27,10 @@ static int worker_cb(void *opaque) static int long_cb(void *opaque) { WorkerTestData *data =3D opaque; - atomic_inc(&data->n); - g_usleep(2000000); - atomic_inc(&data->n); + if (atomic_cmpxchg(&data->n, 0, 1) =3D=3D 0) { + g_usleep(2000000); + atomic_or(&data->n, 2); + } return 0; } =20 @@ -171,7 +172,7 @@ static void do_test_cancel(bool sync) /* Cancel the jobs that haven't been started yet. */ num_canceled =3D 0; for (i =3D 0; i < 100; i++) { - if (atomic_cmpxchg(&data[i].n, 0, 3) =3D=3D 0) { + if (atomic_cmpxchg(&data[i].n, 0, 4) =3D=3D 0) { data[i].ret =3D -ECANCELED; if (sync) { bdrv_aio_cancel(data[i].aiocb); @@ -185,7 +186,7 @@ static void do_test_cancel(bool sync) g_assert_cmpint(num_canceled, <, 100); =20 for (i =3D 0; i < 100; i++) { - if (data[i].aiocb && data[i].n !=3D 3) { + if (data[i].aiocb && atomic_read(&data[i].n) < 4) { if (sync) { /* Canceling the others will be a blocking operation. */ bdrv_aio_cancel(data[i].aiocb); @@ -201,13 +202,22 @@ static void do_test_cancel(bool sync) } g_assert_cmpint(active, =3D=3D, 0); for (i =3D 0; i < 100; i++) { - if (data[i].n =3D=3D 3) { + g_assert(data[i].aiocb =3D=3D NULL); + switch (data[i].n) { + case 0: + fprintf(stderr, "Callback not canceled but never started?\n"); + abort(); + case 3: + /* Couldn't be canceled asynchronously, must have completed. = */ + g_assert_cmpint(data[i].ret, =3D=3D, 0); + break; + case 4: + /* Could be canceled asynchronously, never started. */ g_assert_cmpint(data[i].ret, =3D=3D, -ECANCELED); - g_assert(data[i].aiocb =3D=3D NULL); - } else { - g_assert_cmpint(data[i].n, =3D=3D, 2); - g_assert(data[i].ret =3D=3D 0 || data[i].ret =3D=3D -ECANCELED= ); - g_assert(data[i].aiocb =3D=3D NULL); + break; + default: + fprintf(stderr, "Callback aborted while running?\n"); + abort(); } } } --=20 2.20.1