From nobody Tue Feb 10 21:39:57 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646916847346237.74919357165686; Thu, 10 Mar 2022 04:54:07 -0800 (PST) Received: from localhost ([::1]:33244 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nSIIw-0001Dk-Cx for importer@patchew.org; Thu, 10 Mar 2022 07:54:06 -0500 Received: from eggs.gnu.org ([209.51.188.92]:41360) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nSI9f-0006fm-2T; Thu, 10 Mar 2022 07:44:31 -0500 Received: from [2a00:1450:4864:20::32a] (port=50691 helo=mail-wm1-x32a.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nSI9d-0005Be-DP; Thu, 10 Mar 2022 07:44:30 -0500 Received: by mail-wm1-x32a.google.com with SMTP id l10so3196057wmb.0; Thu, 10 Mar 2022 04:44:28 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id r188-20020a1c2bc5000000b00387c81c32e7sm8063026wmr.8.2022.03.10.04.44.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Mar 2022 04:44:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2cqgz/F/tq6WMZLaHRWJJzZQDdwgOx1eBo+ARCMxfoU=; b=I3UwZECtz27y88tNCRouI1XPB81OFgklj23vZdsVR7hLyVBNwIqE7386sRwBXI77Gs cTRzegjIyvF+KvqMIQt0dgdc/Sii3NZU05TU0xJVfjtvO8+VDPyMqTIg5z7X/y1WYGPG GpdYS83oUP0EpUZUxy5jv3w9NiB2BarQJW9QfrgpBYIkTLyMQhL5MN36htamNSlu7114 JkdkJ2t+inW6YGl0ICDsTKGIlvpBkiJfPQenCLktpv/nnp2g5AS1KCbsatYvr1MucVN4 LuWE0LSOLw9rDFy2mGJrfOOX/fGI7cx+ZHIlC0zb37tQaD7dtRFp+mpAeczfb422qR/J A+XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=2cqgz/F/tq6WMZLaHRWJJzZQDdwgOx1eBo+ARCMxfoU=; b=R1n5KbUHr5Uij0DOf1RFk2K3mu/qVI0RqmMjDIBkA2RONYr+ee/9whBDSaUyW8OFxQ CMf46qhxK/67BEVx3arBVyd3k7jWIE8XqR09faQpSnI1EYcnHxtahIQBwVAWtSd3oVi/ aMgKTulEYBfCwjZtOn89XbGgB9gLgRQ4FwWBkJfrLtrZlb7kXyhU6Y9fm4OuqivfHOVn IGMC6lx+xUWfD9ChqTCkxgYK86ticDdW4P1Wf1ZdTzIOMVPUahPN4EtIYY9fNJDH4h6t uCZiBZF+d/S8XmTsA2U8P6s6WaKVIQ1QTgIo9xhpA02UcW+/k+NGVEerM02eLgqKBiqH /osA== X-Gm-Message-State: AOAM533IWKmqnbBDiWbR67pEQZ0Zb++sEbJp9oqXv/fT//WDm9ZY3uOA vQ5/BSxs06/Bw4/HwodB+LRZRpfNx5M= X-Google-Smtp-Source: ABdhPJzd7bs0Tt9fIYbswdi2WtSws8yj0kuEUNNDPFfVj6//lRmGnlq7ViIHUoOBi07BWRFB3M66ng== X-Received: by 2002:a05:600c:2c49:b0:384:7202:358e with SMTP id r9-20020a05600c2c4900b003847202358emr11660574wmg.108.1646916267397; Thu, 10 Mar 2022 04:44:27 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 09/35] convert qemu-coroutine-sleep.c to stackless coroutines Date: Thu, 10 Mar 2022 13:43:47 +0100 Message-Id: <20220310124413.1102441-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220310124413.1102441-1-pbonzini@redhat.com> References: <20220310124413.1102441-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::32a (failed) Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2a00:1450:4864:20::32a; envelope-from=paolo.bonzini@gmail.com; helo=mail-wm1-x32a.google.com X-Spam_score_int: 0 X-Spam_score: -0.1 X-Spam_bar: / X-Spam_report: (-0.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: hreitz@redhat.com, stefanha@redhat.com, qemu-block@nongnu.org, sguelton@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-ZM-MESSAGEID: 1646916849443100001 Content-Type: text/plain; charset="utf-8" The main change is to qemu_co_sleep_ns_wakeable, which gets the full conversion treatment. It's important to note that variables that escape (have their address taken), such as "QEMUTimer ts" in this case, move entirely to the frame structure and do not have local variables anymore. For the others, always using the frame structure would be inefficient, so they need to be saved and restored. Perhaps "restrict" would be an idea too, I haven't investigated it. qemu_co_sleep almost has a tail call to qemu_coroutine_yield(), except for an assertion after qemu_coroutine_yield() returns. For simplicity and to demonstrate the optimization I'm removing the assertion. Signed-off-by: Paolo Bonzini --- util/qemu-coroutine-sleep.c | 59 ++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index b5bfb4ad18..3d0b1579b3 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -17,7 +17,6 @@ #include "qemu/timer.h" #include "block/aio.h" =20 -#if 0 static const char *qemu_co_sleep_ns__scheduled =3D "qemu_co_sleep_ns"; =20 void qemu_co_sleep_wake(QemuCoSleep *w) @@ -42,7 +41,7 @@ static void co_sleep_cb(void *opaque) qemu_co_sleep_wake(w); } =20 -void coroutine_fn qemu_co_sleep(QemuCoSleep *w) +CoroutineAction qemu_co_sleep(QemuCoSleep *w) { Coroutine *co =3D qemu_coroutine_self(); =20 @@ -56,27 +55,59 @@ void coroutine_fn qemu_co_sleep(QemuCoSleep *w) } =20 w->to_wake =3D co; - qemu_coroutine_yield(); + return qemu_coroutine_yield(); =20 /* w->to_wake is cleared before resuming this coroutine. */ - assert(w->to_wake =3D=3D NULL); + // assert(w->to_wake =3D=3D NULL); } =20 -void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, - QEMUClockType type, int64_t ns) -{ - AioContext *ctx =3D qemu_get_current_aio_context(); - QEMUTimer ts; +struct FRAME__qemu_co_sleep_ns_wakeable { + CoroutineFrame common; + uint32_t _step; + QemuCoSleep *w; + QEMUClockType type; + int64_t ns; + QEMUTimer ts; +}; =20 - aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w); - timer_mod(&ts, qemu_clock_get_ns(type) + ns); +static CoroutineAction co__qemu_co_sleep_ns_wakeable(void *_frame) +{ + struct FRAME__qemu_co_sleep_ns_wakeable *_f =3D _frame; + AioContext *ctx =3D qemu_get_current_aio_context(); + +switch(_f->_step) { +case 0: { + QemuCoSleep *w =3D _f->w; + QEMUClockType type =3D _f->type; + int64_t ns =3D _f->ns; + aio_timer_init(ctx, &_f->ts, type, SCALE_NS, co_sleep_cb, w); + timer_mod(&_f->ts, qemu_clock_get_ns(type) + ns); =20 /* * The timer will fire in the current AiOContext, so the callback * must happen after qemu_co_sleep yields and there is no race * between timer_mod and qemu_co_sleep. */ - qemu_co_sleep(w); - timer_del(&ts); +_f->_step =3D 1; + return qemu_co_sleep(w); +} +case 1: + timer_del(&_f->ts); + goto _out; +} +_out: +stack_free(&_f->common); +return COROUTINE_CONTINUE; +} + +CoroutineAction qemu_co_sleep_ns_wakeable(QemuCoSleep *w, + QEMUClockType type, int64_t ns) +{ + struct FRAME__qemu_co_sleep_ns_wakeable *f; + f =3D stack_alloc(co__qemu_co_sleep_ns_wakeable, sizeof(*f)); + f->w =3D w; + f->type =3D type; + f->ns =3D ns; + f->_step =3D 0; + return co__qemu_co_sleep_ns_wakeable(f); } -#endif --=20 2.35.1