From nobody Sat Oct 25 23:38:23 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 1516054199816132.55519513376407; Mon, 15 Jan 2018 14:09:59 -0800 (PST) Received: from localhost ([::1]:38616 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCx4-0008Gc-UW for importer@patchew.org; Mon, 15 Jan 2018 17:09:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56422) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCve-0007Mo-RR for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebCvc-00069z-4z for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:30 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:42984) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebCvb-00069V-Qy; Mon, 15 Jan 2018 17:08:28 -0500 Received: by mail-wr0-x242.google.com with SMTP id e41so12907793wre.9; Mon, 15 Jan 2018 14:08:27 -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.24 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Jan 2018 14:08:25 -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=WY+0fXHv3T+jeoQ+/hSic6xRlkJ9kLslv3oSWZ0KXVc=; b=jgqHb3041jGpWZ5V+4j0gY0ZX4ZwsF9O6oQbzwJrc0b+xkLcW5rTNteOC/JLm4rXnj AcMN8avbhiVLMrMBmeIuh5aRqPKBoe3kp8S4D+JGqIZIcdBrUspCnygDP2RiaIdDP55H pI842kmdGWe+zKxz+fK2MyoavnNnygi+FxsWOiAWOgM3ArBIdnPjtIL7SlXSNHCvp8h0 RQu0kkVMKE3anqgmg8pz49KtzcoMHGT3MK++hSBtKWMINdKRsEWs+ZFoKAcjsrhBE22l W/k++lXm2DgK3CzBPgWF3J4lkheTGhdgQRXCI9l4zCMjItJZW7g9ypUxpkI0duJkcrhc q1kA== 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=WY+0fXHv3T+jeoQ+/hSic6xRlkJ9kLslv3oSWZ0KXVc=; b=F3apvP/VkDrgzCKn8i8SfnA8ENjwId34eTdC5JixHj2EPCUomNPRAslinBouZP9SPL E/pbnaJ12GBA/hIMlFgPbnhRF87r3oaNBLCBdPN/pg6Yi8MPyTUw/caps9T3dEm2cPKb ylzpa51EPfxUdOmeng7j+HQ6e+eJT/aJdlxbRvacagBPrK8ndSWuZ6uiC1J8qaAw9LGO a3BIfo5jwnpYffxYd6+WBMYO2aQ310eHovmak0yjO2F6WfgYAEmeUtNSHeOIMHhiimsY KdsbzCadZsBSXvxIOoVEDvGolZurt3drWNZSNSHQnRzFXZw59yB7pphfby4wEI6VvXpy FxpA== X-Gm-Message-State: AKwxytcpmVBK3/+jdzk2+/FjaYKmwd42h/XJ3xnXBJuCi45OKCaj6g8O u+If3Gp/0THD6zouqIfxtyTCzRl5 X-Google-Smtp-Source: ACJfBouBCiPObokv8CUmn9sa+uY219i5IAz4zWHpQUlPANyRzpvSjEzd3vNdMzKajFH6hyINJrnkxQ== X-Received: by 10.223.160.209 with SMTP id n17mr228361wrn.25.1516054106373; Mon, 15 Jan 2018 14:08:26 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 15 Jan 2018 23:08:19 +0100 Message-Id: <20180115220822.10156-2-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::242 Subject: [Qemu-devel] [PATCH 1/4] lockable: add QemuLockable 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" QemuLockable is a polymorphic lock type that takes an object and knows which function to use for locking and unlocking. The implementation could use C11 _Generic, but since the support is not very widespread I am instead using __builtin_choose_expr and __builtin_types_compatible_p, which are already used by include/qemu/atomic.h. QemuLockable can be used to implement lock guards, or to pass around a lock in such a way that a function can release it and re-acquire it. The next patch will do this for CoQueue. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- include/qemu/compiler.h | 40 ++++++++++++++++++++++++++ include/qemu/coroutine.h | 4 +-- include/qemu/lockable.h | 75 ++++++++++++++++++++++++++++++++++++++++++++= ++++ include/qemu/thread.h | 5 ++-- include/qemu/typedefs.h | 4 +++ 5 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 include/qemu/lockable.h diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index 340e5fdc09..5179bedb1e 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -111,4 +111,44 @@ #define GCC_FMT_ATTR(n, m) #endif =20 +/* Implement C11 _Generic via GCC builtins. Example: + * + * QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x) + * + * The first argument is the discriminator. The last is the default value. + * The middle ones are tuples in "(type, expansion)" format. + */ + +/* First, find out the number of generic cases. */ +#define QEMU_GENERIC(x, ...) \ + QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +/* There will be extra arguments, but they are not used. */ +#define QEMU_GENERIC_(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ..= .) \ + QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) + +/* Two more helper macros, this time to extract items from a parenthesized + * list. + */ +#define QEMU_FIRST_(a, b) a +#define QEMU_SECOND_(a, b) b + +/* ... and a final one for the common part of the "recursion". */ +#define QEMU_GENERIC_IF_(x, type_then, else_) = \ + __builtin_choose_expr(__builtin_types_compatible_p(x, = \ + QEMU_FIRST_ type_th= en), \ + QEMU_SECOND_ type_then, else_) + +/* CPP poor man's "recursion". */ +#define QEMU_GENERIC1(x, a0, ...) (a0) +#define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC1(x,= __VA_ARGS__)) +#define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC2(x,= __VA_ARGS__)) +#define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC3(x,= __VA_ARGS__)) +#define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC4(x,= __VA_ARGS__)) +#define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC5(x,= __VA_ARGS__)) +#define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC6(x,= __VA_ARGS__)) +#define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC7(x,= __VA_ARGS__)) +#define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC8(x,= __VA_ARGS__)) +#define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF_(x, a0, QEMU_GENERIC9(x= , __VA_ARGS__)) + #endif /* COMPILER_H */ diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index ce2eb73670..8a5129741c 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -121,7 +121,7 @@ bool qemu_coroutine_entered(Coroutine *co); * Provides a mutex that can be used to synchronise coroutines */ struct CoWaitRecord; -typedef struct CoMutex { +struct CoMutex { /* Count of pending lockers; 0 for a free mutex, 1 for an * uncontended mutex. */ @@ -142,7 +142,7 @@ typedef struct CoMutex { unsigned handoff, sequence; =20 Coroutine *holder; -} CoMutex; +}; =20 /** * Initialises a CoMutex. This must be called before any other operation i= s used diff --git a/include/qemu/lockable.h b/include/qemu/lockable.h new file mode 100644 index 0000000000..e280431290 --- /dev/null +++ b/include/qemu/lockable.h @@ -0,0 +1,75 @@ +/* + * Polymorphic locking functions (like templates, but in C) + * + * Copyright Red Hat, Inc. 2017 + * + * Author: Paolo Bonzini + * + * This work is licensed under the terms of the GNU LGPL, version 2 or lat= er. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#ifndef QEMU_LOCKABLE_H +#define QEMU_LOCKABLE_H + +#include "qemu/coroutine.h" +#include "qemu/thread.h" + +typedef void QemuLockUnlockFunc(void *); + +struct QemuLockable { + void *object; + QemuLockUnlockFunc *lock; + QemuLockUnlockFunc *unlock; +}; + +/* This function is used to give link-time errors if an invalid, non-NULL + * pointer type is passed to QEMU_MAKE_LOCKABLE. + */ +void unknown_lock_type(void *); + +/* Auxiliary macros to simplify QEMU_MAKE_LOCABLE. */ +#define QEMU_LOCK_FUNC(x) ((QemuLockUnlockFunc *) \ + QEMU_GENERIC(x, \ + (QemuMutex *, qemu_mutex_lock), \ + (CoMutex *, qemu_co_mutex_lock), \ + (QemuSpin *, qemu_spin_lock), \ + ((x) ? unknown_lock_type : NULL))) + +#define QEMU_UNLOCK_FUNC(x) ((QemuLockUnlockFunc *) \ + QEMU_GENERIC(x, \ + (QemuMutex *, qemu_mutex_unlock), \ + (CoMutex *, qemu_co_mutex_unlock), \ + (QemuSpin *, qemu_spin_unlock), \ + ((x) ? unknown_lock_type : NULL))) + +#define QEMU_MAKE_LOCKABLE_(x) (&(QemuLockable) { \ + .object =3D (x), \ + .lock =3D QEMU_LOCK_FUNC(x), \ + .unlock =3D QEMU_UNLOCK_FUNC(x), \ + }) + +/* QEMU_MAKE_LOCKABLE - Make a polymorphic QemuLockable + * + * @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin). + * + * Returns a QemuLockable object that can be passed around + * to a function that can operate with locks of any kind. + */ +#define QEMU_MAKE_LOCKABLE(x) \ + QEMU_GENERIC(x, \ + (QemuLockable *, x), \ + QEMU_MAKE_LOCKABLE_(x)) + +static inline void qemu_lockable_lock(QemuLockable *x) +{ + x->lock(x); +} + +static inline void qemu_lockable_unlock(QemuLockable *x) +{ + x->unlock(x); +} + +#endif diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 9910f49b3a..a3bc056d89 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -4,7 +4,6 @@ #include "qemu/processor.h" #include "qemu/atomic.h" =20 -typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; typedef struct QemuSemaphore QemuSemaphore; typedef struct QemuEvent QemuEvent; @@ -66,9 +65,9 @@ struct Notifier; void qemu_thread_atexit_add(struct Notifier *notifier); void qemu_thread_atexit_remove(struct Notifier *notifier); =20 -typedef struct QemuSpin { +struct QemuSpin { int value; -} QemuSpin; +}; =20 static inline void qemu_spin_init(QemuSpin *spin) { diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h index 9bd7a834ba..5923849cdd 100644 --- a/include/qemu/typedefs.h +++ b/include/qemu/typedefs.h @@ -19,6 +19,7 @@ typedef struct BusClass BusClass; typedef struct BusState BusState; typedef struct Chardev Chardev; typedef struct CompatProperty CompatProperty; +typedef struct CoMutex CoMutex; typedef struct CPUAddressSpace CPUAddressSpace; typedef struct CPUState CPUState; typedef struct DeviceListener DeviceListener; @@ -86,9 +87,12 @@ typedef struct QEMUBH QEMUBH; typedef struct QemuConsole QemuConsole; typedef struct QemuDmaBuf QemuDmaBuf; typedef struct QEMUFile QEMUFile; +typedef struct QemuLockable QemuLockable; +typedef struct QemuMutex QemuMutex; typedef struct QemuOpt QemuOpt; typedef struct QemuOpts QemuOpts; typedef struct QemuOptsList QemuOptsList; +typedef struct QemuSpin QemuSpin; typedef struct QEMUSGList QEMUSGList; typedef struct QEMUTimer QEMUTimer; typedef struct QEMUTimerListGroup QEMUTimerListGroup; --=20 2.14.3 From nobody Sat Oct 25 23:38:23 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 151605431184938.250304213609866; Mon, 15 Jan 2018 14:11:51 -0800 (PST) Received: from localhost ([::1]:38667 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCyn-0001BK-HQ for importer@patchew.org; Mon, 15 Jan 2018 17:11:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56421) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCve-0007Mn-RI for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebCvd-0006AZ-4I for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:30 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:34134) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebCvc-0006A9-T1; Mon, 15 Jan 2018 17:08:29 -0500 Received: by mail-wr0-x242.google.com with SMTP id 36so13273686wrh.1; Mon, 15 Jan 2018 14:08:28 -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.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Jan 2018 14:08:26 -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=jWMFEBjmM/jy5p7aYeNaR6KVul/E5BuMckwLvXga4Do=; b=sChj33oLPRTAt3Qb9qfkhdOUcpEfiL924gJBxRRCojNDgcL8ZYfHoZ85gK8ZGHH1Li CY0P26m6SQz/9Ikp7PRYHq8TiNir7H232N1xqJ9KaxcVj17M145h1TzIAyfyGFB+LiWg q+d2ISFsYQpC7lmp5rgL4VbrzU7UoFysvJmCETeE7s0kU1rO0RGLFOp/aDxJdnXLAO5T cSEWNfS+y3Rht7P/mm4Dbf41q6kjp8KJsk9EyleLB+3GV16yzzx20o2y3IyjvOOUiMeR 6h4VIL6JTBwlW3MQuPVfdk8mXTb6FrMgWuOiv8nqSw1o7iO7dlRY1pOBsUUkrlqXyaAO /ELA== 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=jWMFEBjmM/jy5p7aYeNaR6KVul/E5BuMckwLvXga4Do=; b=N44M4gW8jJaDBYSfZFC1piDC/9lhv/FQlKSM9C55yJCXAa5lmGOOT381/DnnrvCh6v gXyN952mjKnnbddke8sYAihtuVyXgOdfkEBV1ET6lj63SmmBf5qAAym6W0Q4fgIrxqw1 74ksSXgaYDUTaQ/N5agwvq4BJbsxVy0VacohLJdMrBTV6IheJSZ314HpOuZHpX/I0CEx NW4oxPQBUZ/5QdWwRwhOED8HgAHgwKgkfGwnFfOEJxlNbkr2uxkRq0GITCsosxO0Ig9p zghy+mETfWghipX8uPjuwuUT+wpSsYbsdfp7PYcASoFKhBWsv4qxNjdEfEbKvOwYIaEZ ypUQ== X-Gm-Message-State: AKwxytfh0hS9sl/gokAdNOK9tyieiXDx+Yt0oFXRZC3wsX0/Gu9lzSyy IEYq5GLYzwLhn8c9MNQHhWb6M5Fl X-Google-Smtp-Source: ACJfBovbae9SsXUGB2VaOHP00PkToYDApkfHZaAfVwFj+oqJLv/ehvK4XfJO20wxjTFJA7VEs/ZLwA== X-Received: by 10.223.165.30 with SMTP id i30mr11663752wrb.84.1516054107674; Mon, 15 Jan 2018 14:08:27 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 15 Jan 2018 23:08:20 +0100 Message-Id: <20180115220822.10156-3-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::242 Subject: [Qemu-devel] [PATCH 2/4] coroutine-lock: convert CoQueue to use QemuLockable 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" There are cases in which a queued coroutine must be restarted from non-coroutine context (with qemu_co_enter_next). In this cases, qemu_co_enter_next also needs to be thread-safe, but it cannot use a CoMutex and so cannot qemu_co_queue_wait. Use QemuLockable so that the CoQueue can interchangeably use CoMutex or QemuMutex. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- include/qemu/coroutine.h | 6 +++++- util/qemu-coroutine-lock.c | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 8a5129741c..1e5f0957e6 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -183,7 +183,9 @@ void qemu_co_queue_init(CoQueue *queue); * caller of the coroutine. The mutex is unlocked during the wait and * locked again afterwards. */ -void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex); +#define qemu_co_queue_wait(queue, lock) \ + qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +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. @@ -271,4 +273,6 @@ void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, = int64_t ns); */ void coroutine_fn yield_until_fd_readable(int fd); =20 +#include "qemu/lockable.h" + #endif /* QEMU_COROUTINE_H */ diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 846ff9167f..2a66fc1467 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -40,13 +40,13 @@ void qemu_co_queue_init(CoQueue *queue) QSIMPLEQ_INIT(&queue->entries); } =20 -void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lo= ck) { Coroutine *self =3D qemu_coroutine_self(); QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next); =20 - if (mutex) { - qemu_co_mutex_unlock(mutex); + if (lock) { + qemu_lockable_unlock(lock); } =20 /* There is no race condition here. Other threads will call @@ -60,9 +60,11 @@ void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoM= utex *mutex) /* TODO: OSv implements wait morphing here, where the wakeup * primitive automatically places the woken coroutine on the * mutex's queue. This avoids the thundering herd effect. + * This could be implemented for CoMutexes, but not really for + * other cases of QemuLockable. */ - if (mutex) { - qemu_co_mutex_lock(mutex); + if (lock) { + qemu_lockable_lock(lock); } } =20 --=20 2.14.3 From nobody Sat Oct 25 23:38:23 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 Reviewed-by: Fam Zheng --- 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 From nobody Sat Oct 25 23:38:23 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 1516054410901148.9003062350813; Mon, 15 Jan 2018 14:13:30 -0800 (PST) Received: from localhost ([::1]:38800 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebD0R-0002T5-5F for importer@patchew.org; Mon, 15 Jan 2018 17:13:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56483) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebCvh-0007PN-2c for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebCvg-0006DX-34 for qemu-devel@nongnu.org; Mon, 15 Jan 2018 17:08:32 -0500 Received: from mail-wr0-x243.google.com ([2a00:1450:400c:c0c::243]:46475) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebCvf-0006CW-U2; Mon, 15 Jan 2018 17:08:32 -0500 Received: by mail-wr0-x243.google.com with SMTP id g21so13212118wrb.13; Mon, 15 Jan 2018 14:08:31 -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.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Jan 2018 14:08:30 -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=Y+NRW7Dr5Wv1ew5YBu802on1cGYnQSU4p/voC5el7yM=; b=HOU6fQXdQwRX4TPKsY/KW7iT7PTRvCUFq8QQVMtQcOYpZnQ1fpXwJX8qRGiUuf+kTX 8hjApg5dQvfZ29W/8RpuDtU9pKm3W94kgTLQxO/Cxeer19bGsieW1bzewUC/ZNO+xycX Sj8xyAvXHI0bGPHV+sSVca8TJEx7P9gEeIn4yO+HRtY86g/jUPamkjGpfBKOK4gXSBhO R9W5N2S1oN/Oy4dg/+lCjimkOITGAfjg1YrABSx13xEBwgLQyMvFvob+Q93UnFlUBVf2 K1Kw1Au+4R4Rq83bs8EHb7rN7qYWDJepMLJOo58kX755dJYD+YoikWok2rQTMbEN5PT/ jhtA== 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=Y+NRW7Dr5Wv1ew5YBu802on1cGYnQSU4p/voC5el7yM=; b=bE0QVsvVXZAcZVNr3dLbDBIlqAHk3R8zv27FQO1s25RkpKmYroJfuXADUFkUifID0P 4V36kMh1zdTVYjR8oJIHwFoVgUlnD8nJUdktB+bQ4dNiHRTvcDMaG3UBsaTrnQ4SNvsm hJ3MT83VZJS4xqUkqrNhMigEiDdipT43Bw5E8uEJtN8zapBeEtSQO4owH9+z2ySxzk12 cVJiwlnsuBApm1+l6lpDlIJNw1LYXyLVKkSoeoQ2V0Z3owC0cZspNjwe4CAITGnekNYk JyyMXPG+DGgQaJRec6ROa0v6ztdj4ZBOTscmHKTUU1TgXscNkXDeVDLCZmp474psgnnm AvFg== X-Gm-Message-State: AKwxytdyWEV5YS9RqDy0OJOrcyQ4kVM7yZmZm4NRRaHalFiJg77BVUC1 rKFFQsvp8sN7aIGdZ5moUf8Wunmt X-Google-Smtp-Source: ACJfBou25KnLMNK4z2s2nbJ3wOSAN0sWWYZTpgb/HkJ9vX+oWl6GZVBzcCSWPCMYhQPhtwUL4z+qCw== X-Received: by 10.223.196.147 with SMTP id m19mr22317825wrf.56.1516054110667; Mon, 15 Jan 2018 14:08:30 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 15 Jan 2018 23:08:22 +0100 Message-Id: <20180115220822.10156-5-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::243 Subject: [Qemu-devel] [PATCH 4/4] curl: convert to CoQueue 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" Now that CoQueues can use a QemuMutex for thread-safety, there is no need for curl to roll its own coroutine queue. Coroutines can be placed directly on the queue instead of using a list of CURLAIOCBs. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng --- block/curl.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/block/curl.c b/block/curl.c index 35cf417f59..cd578d3d14 100644 --- a/block/curl.c +++ b/block/curl.c @@ -101,8 +101,6 @@ typedef struct CURLAIOCB { =20 size_t start; size_t end; - - QSIMPLEQ_ENTRY(CURLAIOCB) next; } CURLAIOCB; =20 typedef struct CURLSocket { @@ -138,7 +136,7 @@ typedef struct BDRVCURLState { bool accept_range; AioContext *aio_context; QemuMutex mutex; - QSIMPLEQ_HEAD(, CURLAIOCB) free_state_waitq; + CoQueue free_state_waitq; char *username; char *password; char *proxyusername; @@ -538,7 +536,6 @@ static int curl_init_state(BDRVCURLState *s, CURLState = *state) /* Called with s->mutex held. */ static void curl_clean_state(CURLState *s) { - CURLAIOCB *next; int j; for (j =3D 0; j < CURL_NUM_ACB; j++) { assert(!s->acb[j]); @@ -556,13 +553,7 @@ static void curl_clean_state(CURLState *s) =20 s->in_use =3D 0; =20 - next =3D QSIMPLEQ_FIRST(&s->s->free_state_waitq); - if (next) { - QSIMPLEQ_REMOVE_HEAD(&s->s->free_state_waitq, next); - qemu_mutex_unlock(&s->s->mutex); - aio_co_wake(next->co); - qemu_mutex_lock(&s->s->mutex); - } + qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); } =20 static void curl_parse_filename(const char *filename, QDict *options, @@ -784,7 +775,7 @@ static int curl_open(BlockDriverState *bs, QDict *optio= ns, int flags, } =20 DPRINTF("CURL: Opening %s\n", file); - QSIMPLEQ_INIT(&s->free_state_waitq); + qemu_co_queue_init(&s->free_state_waitq); s->aio_context =3D bdrv_get_aio_context(bs); s->url =3D g_strdup(file); qemu_mutex_lock(&s->mutex); @@ -888,10 +879,7 @@ static void curl_setup_preadv(BlockDriverState *bs, CU= RLAIOCB *acb) if (state) { break; } - QSIMPLEQ_INSERT_TAIL(&s->free_state_waitq, acb, next); - qemu_mutex_unlock(&s->mutex); - qemu_coroutine_yield(); - qemu_mutex_lock(&s->mutex); + qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); } =20 if (curl_init_state(s, state) < 0) { --=20 2.14.3