From nobody Tue Feb 10 05:50:28 2026 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517521086539446.1575628570806; Thu, 1 Feb 2018 13:38:06 -0800 (PST) Received: from localhost ([::1]:36876 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMYV-0003Ha-EJ for importer@patchew.org; Thu, 01 Feb 2018 16:38:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehMU8-0000Ez-PI for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:34:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehMQ9-0003pG-5z for qemu-devel@nongnu.org; Thu, 01 Feb 2018 16:33:32 -0500 Received: from mail-qk0-x241.google.com ([2607:f8b0:400d:c09::241]:35855) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ehMQ8-0003of-Ow; Thu, 01 Feb 2018 16:29:24 -0500 Received: by mail-qk0-x241.google.com with SMTP id d21so21354974qkj.3; Thu, 01 Feb 2018 13:29:24 -0800 (PST) Received: from donizetti.redhat.com ([144.121.20.163]) by smtp.gmail.com with ESMTPSA id q26sm312558qtl.68.2018.02.01.13.29.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Feb 2018 13:29:22 -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=TAyPcChUbYxGtepxj1z0oJi1t5ofsBGEuvVnnYjiqww=; b=b47TiDkxE93eteKAeaDqw6K8OhT4EIqI25OgrK7H9WELfqk9hXrZzdrVI/2YL2E7S6 eU5PmGbZ9WpDnVo+/TEbdqkmMkxT/Rz0ZtU8ODvlFJurgZ6OrEj84UyuQlrv/29scxFc e/RgzmjjSQ0bhfyPXx9kf1TFLmwMWlWVXD0Yt6d7QvKqlEtRO2fPetlHwWTPu9zeulAk 6Iq9NZjovBFhzW4cQvwia3x18TRo6YxOdycEYREfyWJz5Ur92H9zJ0823F5yAkbYMTSA 3DQIenOoOXJSN60fQjdas8dDM9YLvLmrN9ThWL4GrVoRmMwbh5yDQ+l2Lj1t+fasXWXR o/Ew== 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=TAyPcChUbYxGtepxj1z0oJi1t5ofsBGEuvVnnYjiqww=; b=hF/29mIlDaMdEzjgsa2rVARk9e1TUHhcqRlAU8QJYlp9Qwk4hMILnqNOBaQdbIewbi gJWDmJZFUGWawFgmaCWYMca/T8INX5mdB+Ko7DLTv/GBe+gEZwUoMRJQ3sy8ue5qKaGy xpecb43PaRWGx/Mn5Z/CX7gyeBMN7JZEa+spRxTGYLFlUAS+VXPLuukClhuEi+r3/CVm Y/N5HRSo8BmLAXccJ8nkO4zfWKDuEV2FkdxxDap7h1o8FPdhrxXrTojI8mdHeMSStBSK tQjCEAjdUwngsMJ7y8+mtwr9nKTVTPg8ZtgtTDy0XeAB1bI9SdTUI9YcdVe743HU85xl ed6g== X-Gm-Message-State: APf1xPAcNgY8yiF/eA+UijaZzZok2lYHIIKtv7S9Bjwbc0t43TMJfbvQ Kq/zWVfsb2ERzYm/BunpLV+n4VEs X-Google-Smtp-Source: AH8x227/JMWJLaXgoaNCRc9AG7e/MIbbcd4AAnjMkBRM+IUNF45pQAPaF0RwZYJq+SU+Rwp/mH55/g== X-Received: by 10.55.34.72 with SMTP id i69mr1022016qki.287.1517520563814; Thu, 01 Feb 2018 13:29:23 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 16:29:16 -0500 Message-Id: <20180201212917.18131-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180201212917.18131-1-pbonzini@redhat.com> References: <20180201212917.18131-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c09::241 Subject: [Qemu-devel] [PATCH 4/5] 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 | 19 +++++++++++++------ util/qemu-coroutine-lock.c | 10 ++++++++-- 3 files changed, 23 insertions(+), 10 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..3afee7169b 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -188,21 +188,28 @@ 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. - * - * Returns true if a coroutine was restarted, false if the queue is empty. + * Removes the next coroutine from the CoQueue, and wake it up. + * 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 are woken up. */ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue); =20 /** - * Enter the next coroutine in the queue + * Removes the next coroutine from the CoQueue, and wake it up. Unlike + * qemu_co_queue_next, this function releases the lock during aio_co_wake + * because it is meant to be used outside coroutine context; in that case,= the + * coroutine is entered immediately, before qemu_co_enter_next returns. + * + * If used in coroutine context, qemu_co_enter_next is equivalent to + * qemu_co_queue_next. */ -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