From nobody Tue Oct 28 02:08:42 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1516054311035990.5247069095009; Mon, 15 Jan 2018 14:11:51 -0800 (PST) Received: from localhost ([::1]:38668 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCyq-0001Cn-GL for importer@patchew.org; Mon, 15 Jan 2018 17:11:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56444) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCvf-0007N9-Ou for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebCve-0006BO-PF for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:31 -0500 Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]:34136) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebCve-0006Az-Jf; Mon, 15 Jan 2018 17:08:30 -0500 Received: by mail-wr0-x244.google.com with SMTP id 36so13273740wrh.1; Mon, 15 Jan 2018 14:08:30 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-229-84.clienti.tiscali.it. [78.12.229.84]) by smtp.gmail.com with ESMTPSA id n74sm17351039wmi.1.2018.01.15.14.08.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Jan 2018 14:08:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=yh3dOUnhZoHNGEAsQRGM2hFTIDLo+AQPtnCRUbYjNsM=; b=uEZMiFJwz+cBq7N7jIL9WixYdE28dnX7/bxz3Owr55+hxexNw2LGDnt8XBccb50Vq+ ClyFSSmMlwsB/H7meVLZHRAP8KzT98StdHIYxa4pjuzkijxx4tRnhGNWhmzNGUuFCTWO WtTuy4CUS2UGUH0JAqdyGVZgf7aItGUBFi+LoDkBCMQHK/U3yPbBxSFkJcRGO1oPy54H 9eknA1O0eUPrAr7l6+zkq5u16XXiB2+w5kNGyN43d3vfvCPIZAZHZd4sbDyJdYmdJqnY R1nuI1E3XokTwjnS12UFM9PM6kH/n9KJFvcTRWGi9xLPeUyO9mqJwo56KURkgYcdVppB 2lLw== 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 :in-reply-to:references; bh=yh3dOUnhZoHNGEAsQRGM2hFTIDLo+AQPtnCRUbYjNsM=; b=pRCJ3AgBK59RIY6q8Dqi2zKQ1Gdhaok+vndvAhTIWJGgOgfDWN7sMEBZFBPEk0WHxk fw2MkFY+5vcKaqMg5qJc2dtHztmAm2VQIqbA8urhPMfp3Yh8BKukRAsBu5p+nnr5E2FG pXO/h6QtB0xpYAUcH98Co8svJiUTZzDexcrUZaTmUplono5kA4APD6L7Ec2MhqCQHb0H eTWoSlqAtDRV/ERXC8Z0YbcQaCzuYDgi9ATr8zdNe8CXmwvw+VbzXu6Onkx2SSobGK/b xr26vcHKEPX/c7HsjS+UurC0BIlH6bd2ya5V+t04FyJ/AMX6pp7Ynwn3FszO7fpUdRNe AD6Q== X-Gm-Message-State: AKwxytfEske011QiUQ46gntBmbfDK24rcmxaFa8KjsB+geuAJakKk9t8 pmby3Sp2iutiVnFA7niayWO+v5da X-Google-Smtp-Source: ACJfBosyLkNdXTt+hLqySZM9cOf7Gp06TaWFvgEdfnwfSizmyAslEKZA//UlgpKtJVXwprit5EyJwQ== X-Received: by 10.223.180.82 with SMTP id v18mr19951376wrd.179.1516054109342; Mon, 15 Jan 2018 14:08:29 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 15 Jan 2018 23:08:21 +0100 Message-Id: <20180115220822.10156-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180115220822.10156-1-pbonzini@redhat.com> References: <20180115220822.10156-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::244 Subject: [Qemu-devel] [PATCH 3/4] coroutine-lock: make qemu_co_enter_next thread-safe 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: famz@redhat.com, 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) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu_co_queue_next does not need to release and re-acquire the mutex, because the queued coroutine does not run immediately. However, this does not hold for qemu_co_enter_next. Now that qemu_co_queue_wait can synchronize (via QemuLockable) with code that is not running in coroutine context, it's important that code using qemu_co_enter_next can easily use a standardized locking idiom. First of all, qemu_co_enter_next must use aio_co_wake to restart the coroutine. Second, the function gains a second argument, a QemuLockable*, and the comments of qemu_co_queue_next and qemu_co_queue_restart_all are adjusted to clarify the difference. Signed-off-by: Paolo Bonzini --- fsdev/qemu-fsdev-throttle.c | 4 ++-- include/qemu/coroutine.h | 15 ++++++++++----- util/qemu-coroutine-lock.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/fsdev/qemu-fsdev-throttle.c b/fsdev/qemu-fsdev-throttle.c index 49eebb5412..1dc07fbc12 100644 --- a/fsdev/qemu-fsdev-throttle.c +++ b/fsdev/qemu-fsdev-throttle.c @@ -20,13 +20,13 @@ static void fsdev_throttle_read_timer_cb(void *opaque) { FsThrottle *fst =3D opaque; - qemu_co_enter_next(&fst->throttled_reqs[false]); + qemu_co_enter_next(&fst->throttled_reqs[false], NULL); } =20 static void fsdev_throttle_write_timer_cb(void *opaque) { FsThrottle *fst =3D opaque; - qemu_co_enter_next(&fst->throttled_reqs[true]); + qemu_co_enter_next(&fst->throttled_reqs[true], NULL); } =20 void fsdev_throttle_parse_opts(QemuOpts *opts, FsThrottle *fst, Error **er= rp) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 1e5f0957e6..6fdbc837c0 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -188,21 +188,26 @@ void qemu_co_queue_init(CoQueue *queue); void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lo= ck); =20 /** - * Restarts the next coroutine in the CoQueue and removes it from the queu= e. + * Removes the next coroutine from the CoQueue; it will run as soon as the + * current one yields. * - * Returns true if a coroutine was restarted, false if the queue is empty. + * Returns true if a coroutine was removed, false if the queue is empty. */ bool coroutine_fn qemu_co_queue_next(CoQueue *queue); =20 /** - * Restarts all coroutines in the CoQueue and leaves the queue empty. + * Empties the CoQueue; all coroutines in it will run in FIFO orer as soon + * as the current one yields. */ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue); =20 /** - * Enter the next coroutine in the queue + * Immediately enter the next coroutine in the queue. Release the mutex + * while it runs. */ -bool qemu_co_enter_next(CoQueue *queue); +#define qemu_co_enter_next(queue, lock) \ + qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock); =20 /** * Checks if the CoQueue is empty. diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 2a66fc1467..78fb79acf8 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -132,7 +132,7 @@ void coroutine_fn qemu_co_queue_restart_all(CoQueue *qu= eue) qemu_co_queue_do_restart(queue, false); } =20 -bool qemu_co_enter_next(CoQueue *queue) +bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock) { Coroutine *next; =20 @@ -142,7 +142,13 @@ bool qemu_co_enter_next(CoQueue *queue) } =20 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next); - qemu_coroutine_enter(next); + if (lock) { + qemu_lockable_unlock(lock); + } + aio_co_wake(next); + if (lock) { + qemu_lockable_lock(lock); + } return true; } =20 --=20 2.14.3