From nobody Thu May 2 15:05:47 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 ARC-Seal: i=1; a=rsa-sha256; t=1570096972; cv=none; d=zoho.com; s=zohoarc; b=XU0YYDnAw8Lv9yf7SxBfdU07SFIUfmvMrgao6KxBSgX1npbPkWZn8+nwjbJCa83aSafCeV1XJVOGD3LZgfztp4+fDhp41ev59pG6YY38nUJ+5luvg6L5YYtLG7yVYrSXca+sx2h57UPHoeYE9Ek6ZlZuk5BV08AfvvtWS1i4E4s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570096972; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=DAEYct4BxPHsgQHVXklHaubXEaVQN2HXTFBYaYXZbyc=; b=UNXeJ7+AijWcxS7MtwAFG67dmAj9dxWRuXFGVUjCTTLAPzb/uSrPReSSVqlffgohw9aOzfJjcO9pYqY3cQXv/f5P1xzLHp9NZL4SCvQ+cl3M4NMJdcmlaMN5Kfvh83ZgBvN8RQ0sEmpZVSbDOoBcxRHiEv2avazbWfAjdmmqzIw= ARC-Authentication-Results: i=1; mx.zoho.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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1570096972480176.97103944277296; Thu, 3 Oct 2019 03:02:52 -0700 (PDT) Received: from localhost ([::1]:34298 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iFxwW-0005AJ-Sf for importer@patchew.org; Thu, 03 Oct 2019 06:02:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52678) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iFxvN-0004Ye-Di for qemu-devel@nongnu.org; Thu, 03 Oct 2019 06:01:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iFxvM-0007ca-6K for qemu-devel@nongnu.org; Thu, 03 Oct 2019 06:01:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34350) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iFxv4-0007WK-OU; Thu, 03 Oct 2019 06:01:10 -0400 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 mx1.redhat.com (Postfix) with ESMTPS id 641673066EB9; Thu, 3 Oct 2019 10:01:08 +0000 (UTC) Received: from localhost (ovpn-117-189.ams2.redhat.com [10.36.117.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id E33E15C21A; Thu, 3 Oct 2019 10:01:04 +0000 (UTC) From: Stefan Hajnoczi To: Subject: [PATCH] test-bdrv-drain: fix iothread_join() hang Date: Thu, 3 Oct 2019 11:01:03 +0100 Message-Id: <20191003100103.331-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 03 Oct 2019 10:01:08 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Stefan Hajnoczi , qemu-block@nongnu.org, "Dr . David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" tests/test-bdrv-drain can hang in tests/iothread.c:iothread_run(): while (!atomic_read(&iothread->stopping)) { aio_poll(iothread->ctx, true); } The iothread_join() function works as follows: void iothread_join(IOThread *iothread) { iothread->stopping =3D true; aio_notify(iothread->ctx); qemu_thread_join(&iothread->thread); If iothread_run() checks iothread->stopping before the iothread_join() thread sets stopping to true, then aio_notify() may be optimized away and iothread_run() hangs forever in aio_poll(). The correct way to change iothread->stopping is from a BH that executes within iothread_run(). This ensures that iothread->stopping is checked after we set it to true. This was already fixed for ./iothread.c (note this is a different source file!) by commit 2362a28ea11c145e1a13ae79342d76dc118a72a6 ("iothread: fix iothread_stop() race condition"), but not for tests/iothread.c. Fixes: 0c330a734b51c177ab8488932ac3b0c4d63a718a ("aio: introduce aio_co_schedule and aio_co_wake") Reported-by: Dr. David Alan Gilbert Cc: Paolo Bonzini Signed-off-by: Stefan Hajnoczi Reviewed-by: Paolo Bonzini --- tests/iothread.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/iothread.c b/tests/iothread.c index 777d9eea46..13c9fdcd8d 100644 --- a/tests/iothread.c +++ b/tests/iothread.c @@ -55,10 +55,16 @@ static void *iothread_run(void *opaque) return NULL; } =20 -void iothread_join(IOThread *iothread) +static void iothread_stop_bh(void *opaque) { + IOThread *iothread =3D opaque; + iothread->stopping =3D true; - aio_notify(iothread->ctx); +} + +void iothread_join(IOThread *iothread) +{ + aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread); qemu_thread_join(&iothread->thread); qemu_cond_destroy(&iothread->init_done_cond); qemu_mutex_destroy(&iothread->init_done_lock); --=20 2.21.0