From nobody Sat May 4 13:41:25 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; dkim=fail; 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=1571051561; cv=none; d=zoho.com; s=zohoarc; b=JAeH2eJlnuy//pUQ6nU2RDA4a9lD9LhjmJLC6fVvlRyWZnQh5yRA9glM6fi3Ot3CLxp2zGFfMY420lC92izJV5iDuEmsYdcIP/HwZz+D6M8a8QZ2ODUUWZ9Zsc9EVNcYBrGRTNnPgc5GYlIO4wokWRapOAjG0hV69wuYplI969w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571051561; 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; bh=fpVCniRK6qIUkUWwjCvekvk/Zya7KZVpgagyAg4Va7E=; b=Iiod9k2DeCWJ+0gm5G69ylSbJlVlEqoeYNwWAq7aP346DFZsYjuxBp40SaMya7S3+AG/+usXPniWPcCJjnRUdUgv2oGmxxySXGgkaTefzB1Al16GCDTWar4SAY7rm76MripOJ4tMTSjMIgnbuLX/8xhZ/66HOa19PnjBBU/gabU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; 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 (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571051561769842.9682861265709; Mon, 14 Oct 2019 04:12:41 -0700 (PDT) Received: from localhost ([::1]:47496 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJyHD-0005hZ-GM for importer@patchew.org; Mon, 14 Oct 2019 07:12:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45295) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJy1i-0005y8-BA for qemu-devel@nongnu.org; Mon, 14 Oct 2019 06:56:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iJy1g-0004Zq-4n for qemu-devel@nongnu.org; Mon, 14 Oct 2019 06:56:34 -0400 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:56149) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iJy1e-0004VI-QH; Mon, 14 Oct 2019 06:56:32 -0400 Received: by mail-wm1-x343.google.com with SMTP id a6so16735559wma.5; Mon, 14 Oct 2019 03:56:28 -0700 (PDT) Received: from donizetti.lan ([2001:b07:6468:f312:ddc7:c53c:581a:7f3e]) by smtp.gmail.com with ESMTPSA id s10sm33921404wmf.48.2019.10.14.03.56.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2019 03:56:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fpVCniRK6qIUkUWwjCvekvk/Zya7KZVpgagyAg4Va7E=; b=P2ej4W3YzxaUjqqMxBZ0lbQJLWqgq6A7WyXgIMKImSHEK5t08tedvQ1bFuT3DFdIPY PL6mLWCZAgeAVPKYWUIFTWEv6YhMALQMQ9m8J10pU73cksnzRhRUTQab/CqCshGcD+qe AjIPWQUEYlCn6PhpvhfNdv5UT1YlibT2g7UQY7lnyeBtY9sb0FU2Od33pMQBwfHNf92D Ie9LXyJDAcZN0QapP7gXs3gk+RobLol4rKukwsBz2GgdKZ2pYU01eCs06w7fAdhoTgYs i70I9CN76EQ6xMZD91lCGtArhECUb81ouN/E1arVFUlPmFEuQx9VR/qeqzSjC/dhSwwt 5cRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=fpVCniRK6qIUkUWwjCvekvk/Zya7KZVpgagyAg4Va7E=; b=SFyP8OI7KzQWg6+cUsxhaRdpwh/vohoXe4gXZETvPoHk2j8Vqx8P0dixGQ4Ru9wXF1 WlnM3g3Z4iXNJDbMHTT9pyoW0N18tUp3yFHZ5VI2EPjvBO9yzRjOTJQZx0NjtXdMVCGF lgSbVGdSF5UM7hZEeSAetAiHZ84alO4qFu2VZEjirxtZFCg7t5aCoDs6wUsi3PXgS9fd HpDLYJsLG3oUjd27KwtbwIUHt6Q4npPFGQjiFHF24h7G5eNP3vqRkBYz06WwRYANaQFc GY9OM5o0PHzxFp43eGQU+AIXzn52npSiHn0GbzkNv1jj5jAO7W8Gxoov+o04uCiEQOZh ehTg== X-Gm-Message-State: APjAAAVk+KwiUHCJM5ILaaRv78y8je58PMqwLTYgaFjWViM/ccjWm4He IOiOWAIGX6Wdi+7lxO88JyIRLMj7 X-Google-Smtp-Source: APXvYqyYAasWyajZoapl0hwR2HCIm238IshjLcUPX+Dp0i8UCvrIbUodFSyDg6FrEovHeO0E6BzEeA== X-Received: by 2002:a1c:a444:: with SMTP id n65mr15078584wme.111.1571050587280; Mon, 14 Oct 2019 03:56:27 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH] tests: fix iothread_join() race condition Date: Mon, 14 Oct 2019 12:56:27 +0200 Message-Id: <20191014105627.26977-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::343 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: Peter Maydell , qemu-block@nongnu.org, stefanha@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This is a similar race condition to the one fixed in commit 2362a28ea1 ("iothread: fix iothread_stop() race condition", 2017-12-19), which in this= case was exposed as a failure in test-aio-multithread. Here, the iothread->stop= ping flag was set too soon, and the AioContext could be destroyed before the last round of entering scheduled coroutines and bottom halves. The fix is similar. Reported-by: Peter Maydell Signed-off-by: Paolo Bonzini --- tests/iothread.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/iothread.c b/tests/iothread.c index 777d9eea46..1e6d3e2218 100644 --- a/tests/iothread.c +++ b/tests/iothread.c @@ -26,6 +26,7 @@ struct IOThread { QemuMutex init_done_lock; QemuCond init_done_cond; /* is thread initialization done? */ bool stopping; + bool running; }; =20 static __thread IOThread *my_iothread; @@ -47,7 +48,7 @@ static void *iothread_run(void *opaque) qemu_cond_signal(&iothread->init_done_cond); qemu_mutex_unlock(&iothread->init_done_lock); =20 - while (!atomic_read(&iothread->stopping)) { + while (iothread->running) { aio_poll(iothread->ctx, true); } =20 @@ -55,10 +56,26 @@ static void *iothread_run(void *opaque) return NULL; } =20 +/* Runs in iothread_run() thread */ +static void iothread_stop_bh(void *opaque) +{ + IOThread *iothread =3D opaque; + + iothread->running =3D false; /* stop iothread_run() */ +} + void iothread_join(IOThread *iothread) { + /* Forbid reentering iothread_join. */ + assert(!iothread->stopping); iothread->stopping =3D true; - aio_notify(iothread->ctx); + + /* + * Force the loop to run once more, so that already scheduled bottom + * halves and coroutines are executed. + */ + 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); @@ -76,6 +93,7 @@ IOThread *iothread_new(void) iothread, QEMU_THREAD_JOINABLE); =20 /* Wait for initialization to complete */ + iothread->running =3D true; qemu_mutex_lock(&iothread->init_done_lock); while (iothread->ctx =3D=3D NULL) { qemu_cond_wait(&iothread->init_done_cond, --=20 2.21.0