From nobody Tue May 14 15:57:53 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1621246506; cv=none; d=zohomail.com; s=zohoarc; b=nuK/gJMDMslXhIpu2wDQjSSjfSWzieAjKrd5Z/6KEq4UPsngcuDLcD6UH+nbC7Kuw0hknLNIj2F6aigW0v5r+PeqPWNwtBIzh4IrXY/HTdZyil4P7377iOLvtjc6uWUnzWC07eGjmJ3WfO6/ZAVJGtlmAtYCSph6oqRGtX+f8QA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621246506; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=GBqTDXA9Nz0xhg9QJ+rdI3aq/c/Qwf8zhyPVSPAzEfk=; b=U6+FdpkWqb0u9iXX9nwP/4mIvwUAcBuJV2zK5ENrdDABsjCtQkTT46ter+okdkdRO0euiZdlxgGSBsKNVt9BtezBRD6oRgbuVftjnCHx6IUQqxWoLlmiKscsOywctuCzWdkgg8eeSHvGUJzFmjcZ2R3lJPu+ZHdh/gT9shAyywk= ARC-Authentication-Results: i=1; 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 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 1621246506993987.0506834256646; Mon, 17 May 2021 03:15:06 -0700 (PDT) Received: from localhost ([::1]:34176 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liaHB-0006eb-OV for importer@patchew.org; Mon, 17 May 2021 06:15:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42984) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lia8J-0004kx-6z; Mon, 17 May 2021 06:05:56 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]:43975) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lia8H-0001eR-Ei; Mon, 17 May 2021 06:05:54 -0400 Received: by mail-ej1-x62f.google.com with SMTP id l4so8288016ejc.10; Mon, 17 May 2021 03:05:52 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id h9sm10659926ede.93.2021.05.17.03.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 03:05:51 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=GBqTDXA9Nz0xhg9QJ+rdI3aq/c/Qwf8zhyPVSPAzEfk=; b=MA0f/9tP/l2sJdo1WEY4KtnChAhryCR6i3xLTi/9twLM6sFrExYO0SKkCxBGR9zwxj dfToVJjJaaXCmjFnTwNw9GnjdBMFzqA6Wo1aIZwTZ5gxf8Cq8uc/EV6NDmz3N6OURk6v 8/ZZ+NSCezOSi/ZfvNpQEsFz3fRr05SnRG1HgBozRo/U7HOLxiwFN+8FrKzndH2s3w7F U1KgNFy8ns1PvZspuR1HWHmLVBs+n836ZeRfKDWJT5fUGDSr/DMdCJAHB8VpUYNLlRHQ +rOEbouRYCix5KTS09Cjp5VMxbS/uBHw3x8EScBw79LYvKE5/tRxQbmCIfgxeB9g++HS A3Kg== 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:mime-version:content-transfer-encoding; bh=GBqTDXA9Nz0xhg9QJ+rdI3aq/c/Qwf8zhyPVSPAzEfk=; b=rgunenQ48p2sqt0BSh/vcoVu7PsNDaRr9mQD2hFSDa91YRhvU6Ul8z382w3lNplRhh r1lBtWUbTFJjdDFc85KtuLmahVLHrit3Rh/QL7I8qOmFFEjeJIuh/9xKvuGgoaRxX4Ev yopmbpcnQTm1lwhWt+VdZ4p+8eGJHL3xasNVV3B863kb/GV5alPsFeaG9RwwDwhAxMKJ wJddc92ptYdmofo4N/53AUMUkuMPR5LoC5qVzCgdjxcZvUgoJp3MzGTUFYdXHSHe9br4 JDRfsjPANSM2pelIndnkhW0nISS7/82olNn5iehZ5KjxLAowWbN9KY5WNih48gjURLRx g/Ng== X-Gm-Message-State: AOAM532vrA9DKuAlWoCm6AnmdwRL12b9frk9BR2f6wvj+38fpZMtkN4y mqRXGjzuvF64TA2xNCrzi2g16UL5AcUYSw== X-Google-Smtp-Source: ABdhPJzvFpGGcbDUqh/hJJYPYn6SIFX33ETmfd1QfmsjB1AgfF2NFusyStSR4WAHizZzeOGHBV8RAQ== X-Received: by 2002:a17:907:9715:: with SMTP id jg21mr17108641ejc.52.1621245951480; Mon, 17 May 2021 03:05:51 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH v2 1/6] coroutine-sleep: use a stack-allocated timer Date: Mon, 17 May 2021 12:05:43 +0200 Message-Id: <20210517100548.28806-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517100548.28806-1-pbonzini@redhat.com> References: <20210517100548.28806-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::62f; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x62f.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: eesposit@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com, qemu-block@nongnu.org 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" The lifetime of the timer is well-known (it cannot outlive qemu_co_sleep_ns_wakeable, because it's deleted by the time the coroutine resumes), so it is not necessary to place it on the heap. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini --- util/qemu-coroutine-sleep.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 8c4dac4fd7..eec6e81f3f 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -21,7 +21,7 @@ static const char *qemu_co_sleep_ns__scheduled =3D "qemu_= co_sleep_ns"; =20 struct QemuCoSleepState { Coroutine *co; - QEMUTimer *ts; + QEMUTimer ts; QemuCoSleepState **user_state_pointer; }; =20 @@ -35,7 +35,7 @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) if (sleep_state->user_state_pointer) { *sleep_state->user_state_pointer =3D NULL; } - timer_del(sleep_state->ts); + timer_del(&sleep_state->ts); aio_co_wake(sleep_state->co); } =20 @@ -50,7 +50,6 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType= type, int64_t ns, AioContext *ctx =3D qemu_get_current_aio_context(); QemuCoSleepState state =3D { .co =3D qemu_coroutine_self(), - .ts =3D aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &state), .user_state_pointer =3D sleep_state, }; =20 @@ -63,10 +62,11 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockTy= pe type, int64_t ns, abort(); } =20 + aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state); if (sleep_state) { *sleep_state =3D &state; } - timer_mod(state.ts, qemu_clock_get_ns(type) + ns); + timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); if (sleep_state) { /* @@ -75,5 +75,4 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType= type, int64_t ns, */ assert(*sleep_state =3D=3D NULL); } - timer_free(state.ts); } --=20 2.31.1 From nobody Tue May 14 15:57:53 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1621246090; cv=none; d=zohomail.com; s=zohoarc; b=OUE0nbE+NVK0LoRqhPjfejPIcx5byJpE5wZa6A1W2+NmZPfFuxz3HA4DPSI2zHwDfKcRAjiQB7Bv2iFGbWccKbDFv36qGcsWbsYsJxTDxw6fH4zJpci144hTjQH12sAylvvaiYWx/lsPCxvjc32pDlvKul38UnvZ27IVeGNbtY8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621246090; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=f3bgpbEohNOOBOFnBrKBrT5ery7OficY23T1F2PzmYo=; b=IS+WpvEPywwKGXGJ5iiPgAWjF+0iYlbiDMVh3jP9jv41XpmRNJPA2pT+wBm2+Akcy3nwZoS72o1rFxGeGnn4udlJvhFUm3xQ2oAjZS/lPMevt/uVrcCCNKQXFghsqLcnJ9YhtpwLh0QxS4qCMG+3E3VHw2m9p14q5S+wYzKxX0Y= ARC-Authentication-Results: i=1; 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 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 162124609047383.62430900748814; Mon, 17 May 2021 03:08:10 -0700 (PDT) Received: from localhost ([::1]:51094 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liaAT-000736-AT for importer@patchew.org; Mon, 17 May 2021 06:08:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43014) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lia8L-0004mi-0G; Mon, 17 May 2021 06:05:57 -0400 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]:44858) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lia8I-0001fK-1a; Mon, 17 May 2021 06:05:56 -0400 Received: by mail-ej1-x62d.google.com with SMTP id lz27so8283347ejb.11; Mon, 17 May 2021 03:05:53 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id h9sm10659926ede.93.2021.05.17.03.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 03:05:52 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=f3bgpbEohNOOBOFnBrKBrT5ery7OficY23T1F2PzmYo=; b=ITMwkaOo6yy1XE3EjRD+L42V3BMweWWEGDqdC9GiGJOWk3dlEZx/2mJmYB+v9qN2ln FenIKH6D/k6gcOyskVbZGKNS3/bLWTUEp+mef4K9fZereCrxMYjdfd1rRbXVZFVBEGYR sk6ri3TVt8UszMWDVD1XlwM0IJ+y1UmjmV7GLmqispbxhhTTy/0WciES+jPOLNjrlD4c VsnKCZv/k+1SB+Pft8DcAhsqO6A2AR4BsXxKg21pRiT5lGA9mFmeBmToG6nLHtJkT6YC j8aIzrvf3WeJrNtBD6Sa2dHmmjVilRbTvMGNdzD4cIUpru0EQf8e4dewAz+XK9GeqTFv d1xg== 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:mime-version:content-transfer-encoding; bh=f3bgpbEohNOOBOFnBrKBrT5ery7OficY23T1F2PzmYo=; b=EYOc3varTVbBVWuVujjM+Trbkja1z1ZQCez/dgy+GlExNuRIxGJYgPJUoETvy+AaQ7 U/OOICkzuCZNxqgAc6W8liaNkmP4aqg5qkfgIkUMAJhKoi+IZ/wjQBqUfhHKSJ54c66Q CX2oh9B76cry4yhd3HacGlt9MmJqOLefz54Ycr8t4mddDN1T6Dio9WxtZdszcOBv7ACQ UqvIZJvCLfSL+gQt+ZFyiHAIRc2796tlsX/6jXyRPLFi9+f6GqqSsMHl/mUysjd0RPNi h32g6MpppGKenCVU7Nrc4GEUqGcmK2ZXMBwJ6C+d+uA+7KoSn3cSSgqKlbakdEC+47tb gYJg== X-Gm-Message-State: AOAM531ADmJ9e5BcEt4fCNCSNpgGtspkkxq3DRogpzaKig6ywJCaaBge i8S46aQgi6c7MQmUfqL/U/8U4z5n2Elzig== X-Google-Smtp-Source: ABdhPJwX6CtdnaM+EkQn7bQq6q3DzebhFqNGdjQtrm7zJuSgut1TSgKCRHcO/067pv9c+nw3/s8kRw== X-Received: by 2002:a17:906:e210:: with SMTP id gf16mr4812659ejb.472.1621245952399; Mon, 17 May 2021 03:05:52 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH v2 2/6] coroutine-sleep: disallow NULL QemuCoSleepState** argument Date: Mon, 17 May 2021 12:05:44 +0200 Message-Id: <20210517100548.28806-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517100548.28806-1-pbonzini@redhat.com> References: <20210517100548.28806-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::62d; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x62d.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: eesposit@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com, qemu-block@nongnu.org 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" Simplify the code by removing conditionals. qemu_co_sleep_ns can simply point the argument to an on-stack temporary. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini --- include/qemu/coroutine.h | 5 +++-- util/qemu-coroutine-sleep.c | 18 +++++------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index ce5b9c6851..c5d7742989 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -295,7 +295,7 @@ typedef struct QemuCoSleepState QemuCoSleepState; =20 /** * Yield the coroutine for a given duration. During this yield, @sleep_sta= te - * (if not NULL) is set to an opaque pointer, which may be used for + * is set to an opaque pointer, which may be used for * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when = the * timer fires. Don't save the obtained value to other variables and don't= call * qemu_co_sleep_wake from another aio context. @@ -304,7 +304,8 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockTy= pe type, int64_t ns, QemuCoSleepState **sleep_state= ); static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64= _t ns) { - qemu_co_sleep_ns_wakeable(type, ns, NULL); + QemuCoSleepState *unused =3D NULL; + qemu_co_sleep_ns_wakeable(type, ns, &unused); } =20 /** diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index eec6e81f3f..3f6f637e81 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -32,9 +32,7 @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) qemu_co_sleep_ns__scheduled, NU= LL); =20 assert(scheduled =3D=3D qemu_co_sleep_ns__scheduled); - if (sleep_state->user_state_pointer) { - *sleep_state->user_state_pointer =3D NULL; - } + *sleep_state->user_state_pointer =3D NULL; timer_del(&sleep_state->ts); aio_co_wake(sleep_state->co); } @@ -63,16 +61,10 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockTy= pe type, int64_t ns, } =20 aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state); - if (sleep_state) { - *sleep_state =3D &state; - } + *sleep_state =3D &state; timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); - if (sleep_state) { - /* - * Note that *sleep_state is cleared during qemu_co_sleep_wake - * before resuming this coroutine. - */ - assert(*sleep_state =3D=3D NULL); - } + + /* qemu_co_sleep_wake clears *sleep_state before resuming this corouti= ne. */ + assert(*sleep_state =3D=3D NULL); } --=20 2.31.1 From nobody Tue May 14 15:57:53 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1621246958; cv=none; d=zohomail.com; s=zohoarc; b=llaabAsevwcZnlwfPgmWh3tkLbsSBvdhU0T1nKtbYyu7goSV9nPmazxSuT9sM7mSI4aAp5LrIsdwNcNIp+rdzqWUq0hfkypn7D7pXx5WhfpPjxsJb6dN+Po7WPpJ9LgYhjid+Dn4WLss7mqJ7Y+Mb42BC875Tytle/S1n2in0fQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621246958; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Ldi1B45Le+F//0iymW8+X/cGEwD6/csDFGa23RugWNg=; b=gz1W90fvRHZN6/1LkxJ3geeJixBkNhzW4GkhQNLg34bPHd3q+672FafjquQJ1k5HtZaEErXuFO///qs0CaBf+L4Sr22/1hhOdvlzGbPXmsUuizTihNSIfGZeQv85sygdlU44jxLxPd/cnJGjrjuRZ5VvHhev7cN02vWsZchS+GI= ARC-Authentication-Results: i=1; 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 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 16212469581941018.9231543978331; Mon, 17 May 2021 03:22:38 -0700 (PDT) Received: from localhost ([::1]:47702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liaOT-0007XC-3U for importer@patchew.org; Mon, 17 May 2021 06:22:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lia8M-0004oy-Qi; Mon, 17 May 2021 06:05:59 -0400 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]:44848) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lia8J-0001gM-4B; Mon, 17 May 2021 06:05:57 -0400 Received: by mail-ed1-x530.google.com with SMTP id t15so6124882edr.11; Mon, 17 May 2021 03:05:54 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id h9sm10659926ede.93.2021.05.17.03.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 03:05:52 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ldi1B45Le+F//0iymW8+X/cGEwD6/csDFGa23RugWNg=; b=G5r2IDN9Do5gxYTrVYrO63oO8hKjlaXDZoydm4AcBK4KjPlqPmQggnuZ9bGKJl4LKT T3inZKVXkh8TOF5p5/MfeIW/Z5gvyrLwHp7CO0JYBgTDYrPaazSw+ak14fIEAfd/yC+O 61/TLsbXrkUGh90y4ivWaeqyLOHF7s1Zf2udW3mYXOMV0ejErY7koQeSpqtHsEcuqsax 3rHl7MqIFysHxbK1lyrgOHZyEj9EeNkjtUIM+rPB1bbe6MFsnheO+ArDWEcXju1lUXu9 qmJ+lnYpX81RxSCQy6qSbjSvSjHELAkCAUaYWINpUH9i9YUec4vyhScxHmgYJOQZg5XF VEXA== 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:mime-version:content-transfer-encoding; bh=Ldi1B45Le+F//0iymW8+X/cGEwD6/csDFGa23RugWNg=; b=VSx5hayrjPiTOm1mXdHbl45LsZwSPcFa/Sir8rLibaQd8E8hr0/TP60lBK8Q2WwUlf k7Yw6fefutnnH0gJdSSPoN4eFgbVUPeY9D9U8OytzK3tXIoxNcuZwMs/HpEv0l3v7Ehm B5/l0M7ouHMKMUeFj7LWse3OxGrd9b9rsckjFMv+gLUJ7/VTZNLOk+/S/3QbWCf2VoLC j2R/QvZw83f7lXC9iYpdK7h6UgTulEmAyh/YIFNOyZ5v6SDoGSpY/NpSplNwbFU7Qf3V 9edJpW7SMxUOcFzZX3VI6+VPJDYK3Ds5NLIoWRp1HFK/yJfUyJcKolfvNimcVWM+nb8U fGcw== X-Gm-Message-State: AOAM533xlLLpINNF1hcFR0rJqPsgR15B1B5+b8nvxRqfXIg8//9l+OhP JaTlTfFB+r+f1lKuxDO5TRyfdqdhKNYp4w== X-Google-Smtp-Source: ABdhPJwO6Q/ebHkoVRUzxUX5x/OQPNXFdkP/vl4tTW42L/vdi3+fsVaIzErXC0PzcXxXtV9Luxycog== X-Received: by 2002:a50:d69c:: with SMTP id r28mr21435621edi.64.1621245953471; Mon, 17 May 2021 03:05:53 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH v2 3/6] coroutine-sleep: allow qemu_co_sleep_wake that wakes nothing Date: Mon, 17 May 2021 12:05:45 +0200 Message-Id: <20210517100548.28806-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517100548.28806-1-pbonzini@redhat.com> References: <20210517100548.28806-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::530; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x530.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: eesposit@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com, qemu-block@nongnu.org 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" All callers of qemu_co_sleep_wake are checking whether they are passing a NULL argument inside the pointer-to-pointer: do the check in qemu_co_sleep_wake itself. As a side effect, qemu_co_sleep_wake can be called more than once and it will only wake the coroutine once; after the first time, the argument will be set to NULL via *sleep_state->user_state_pointer. However, this would not be safe unless co_sleep_cb keeps using the QemuCoSleepState* directly, so make it go through the pointer-to-pointer instead. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini --- block/block-copy.c | 4 +--- block/nbd.c | 8 ++------ util/qemu-coroutine-sleep.c | 21 ++++++++++++--------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index 9b4af00614..f896dc56f2 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -674,9 +674,7 @@ out: =20 void block_copy_kick(BlockCopyCallState *call_state) { - if (call_state->sleep_state) { - qemu_co_sleep_wake(call_state->sleep_state); - } + qemu_co_sleep_wake(call_state->sleep_state); } =20 /* diff --git a/block/nbd.c b/block/nbd.c index 1d4668d42d..1c6315b168 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -289,9 +289,7 @@ static void coroutine_fn nbd_client_co_drain_begin(Bloc= kDriverState *bs) BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; =20 s->drained =3D true; - if (s->connection_co_sleep_ns_state) { - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); - } + qemu_co_sleep_wake(s->connection_co_sleep_ns_state); =20 nbd_co_establish_connection_cancel(bs, false); =20 @@ -330,9 +328,7 @@ static void nbd_teardown_connection(BlockDriverState *b= s) =20 s->state =3D NBD_CLIENT_QUIT; if (s->connection_co) { - if (s->connection_co_sleep_ns_state) { - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); - } + qemu_co_sleep_wake(s->connection_co_sleep_ns_state); nbd_co_establish_connection_cancel(bs, true); } if (qemu_in_coroutine()) { diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 3f6f637e81..3ae2b5399a 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -27,19 +27,22 @@ struct QemuCoSleepState { =20 void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) { - /* Write of schedule protected by barrier write in aio_co_schedule */ - const char *scheduled =3D qatomic_cmpxchg(&sleep_state->co->scheduled, - qemu_co_sleep_ns__scheduled, NU= LL); + if (sleep_state) { + /* Write of schedule protected by barrier write in aio_co_schedule= */ + const char *scheduled =3D qatomic_cmpxchg(&sleep_state->co->schedu= led, + qemu_co_sleep_ns__schedule= d, NULL); =20 - assert(scheduled =3D=3D qemu_co_sleep_ns__scheduled); - *sleep_state->user_state_pointer =3D NULL; - timer_del(&sleep_state->ts); - aio_co_wake(sleep_state->co); + assert(scheduled =3D=3D qemu_co_sleep_ns__scheduled); + *sleep_state->user_state_pointer =3D NULL; + timer_del(&sleep_state->ts); + aio_co_wake(sleep_state->co); + } } =20 static void co_sleep_cb(void *opaque) { - qemu_co_sleep_wake(opaque); + QemuCoSleepState **sleep_state =3D opaque; + qemu_co_sleep_wake(*sleep_state); } =20 void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, @@ -60,7 +63,7 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType= type, int64_t ns, abort(); } =20 - aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state); + aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_stat= e); *sleep_state =3D &state; timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); --=20 2.31.1 From nobody Tue May 14 15:57:53 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1621246680; cv=none; d=zohomail.com; s=zohoarc; b=RlORx8+Bu0P3uZonNb3Xv1bx7/S3AxK5pkIpst7wiVxOMzab1/UxhuDKebwG+IjdEFFdG3Ii4T2xmIJIO4f+4UOGcjUXmFuXcNYDSjdDsPzpomtWrpecjRSXLIzLg/5gEnH0bM2dXwtQcqRZvSZeVQ4iM8l0RXb5VQ7wxMG8IB8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621246680; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=K8NwIGTy4xrdgQYoJbPMNCQEvseYJEQuulAUnjQxH2U=; b=Fu8mq3f6ca44VtCB1ILiUZZ/TSduujn0HDP2T0NYxJQcu8NJaKfh+q2pQ7yu+Tm6M15OY0MWUrQcd8YJVqPxvFIL3FJPjr/VX4Mj92/zTj4/ZGVkPN56u6huGNRx8zAZfFM1KXQp/lIbc9kNHPtzD8rApHF8hOQrxUDbCcSRc4E= ARC-Authentication-Results: i=1; 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 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 1621246680437443.351776249071; Mon, 17 May 2021 03:18:00 -0700 (PDT) Received: from localhost ([::1]:37924 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liaJz-0000rl-FY for importer@patchew.org; Mon, 17 May 2021 06:17:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43038) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lia8M-0004ox-Qk; Mon, 17 May 2021 06:05:59 -0400 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]:33556) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lia8K-0001gT-4u; Mon, 17 May 2021 06:05:58 -0400 Received: by mail-ed1-x533.google.com with SMTP id b17so6196692ede.0; Mon, 17 May 2021 03:05:55 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id h9sm10659926ede.93.2021.05.17.03.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 03:05:53 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=K8NwIGTy4xrdgQYoJbPMNCQEvseYJEQuulAUnjQxH2U=; b=vKNDGrfGK1Rs7ybhlG0dGvOdfjBsQiWpQfZhYuxHkl8sU4PEA8o5QkqyzZoOxZBh/t tYrvaIpwxcyCl2IRxMcULxJE0W95p0Mr8GEi3lbJ5MZyl9YvLkQoLKqha4HjdbMff04r ARjQhQeEAdweMEwWh9fGLhzsJPxLpIedIZk08pWL6LactLv91XnYTq2fvNXWVGZ2v5+I qVOBlkHQtJRgSqeQ0Tuym2YiceQyi1aQ79sHZ9oy9sNvmQEJABXmly1mvMzoGnO/B1MZ meBYXZOdCj40DCPyLla4phWN+Oia7ZYIRyFOlxNoeRxDb0tfR0lDIAo85lBQoPb2L1px 04Rw== 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:mime-version:content-transfer-encoding; bh=K8NwIGTy4xrdgQYoJbPMNCQEvseYJEQuulAUnjQxH2U=; b=q99rTmO4BlC+YwQd64LAPqbaU9JeKzCuzjEglxcX1aEfcxoCgkCwd3mJAOQ/h70Akq IeFM9mbm9TUgkrbD+v6VGGoD0MS/lEcdP95OhqkA0wmT0PRXOSjOFVp2xrFcUhiKZ+Mc eSUBr1Q/3f/rasQzbywbg1a94gzIvrYFreGenziy9THaC6KDJhnXbb7XaXCeWDkbiK+M zlkBHodYTPiHmLEKUQuMRQVWikkIba1t/DX7r9KaCYO4LNubcLAffZ1iuz89kPUVgWQy O8f3Vw5m8MjgR+OHqrqIYPd3iCHvNynywQAL1xM9VtNTs4xORItRzavdtAcslyli0DbB wRLw== X-Gm-Message-State: AOAM532AEhECZrJr/FVATYOfNs1SsR5WmwVGHXJ7dNovPeZvS1vzufje VPFiDJO51I2JQ0i4IsZC+9jywxeY2OCfyQ== X-Google-Smtp-Source: ABdhPJzlew+TMhTM6q88TDDAnuh9w7uJfB6radI/G9aohbnhboOWcbc28SuW7rjkc3TT3BGw6b3wPg== X-Received: by 2002:aa7:dc49:: with SMTP id g9mr27333409edu.160.1621245954376; Mon, 17 May 2021 03:05:54 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH v2 4/6] coroutine-sleep: move timer out of QemuCoSleepState Date: Mon, 17 May 2021 12:05:46 +0200 Message-Id: <20210517100548.28806-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517100548.28806-1-pbonzini@redhat.com> References: <20210517100548.28806-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::533; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x533.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: eesposit@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com, qemu-block@nongnu.org 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 simplification is enabled by the previous patch. Now aio_co_wake will only be called once, therefore we do not care about a spurious firing of the timer after a qemu_co_sleep_wake. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini --- util/qemu-coroutine-sleep.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 3ae2b5399a..1d25019620 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -21,7 +21,6 @@ static const char *qemu_co_sleep_ns__scheduled =3D "qemu_= co_sleep_ns"; =20 struct QemuCoSleepState { Coroutine *co; - QEMUTimer ts; QemuCoSleepState **user_state_pointer; }; =20 @@ -34,7 +33,6 @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) =20 assert(scheduled =3D=3D qemu_co_sleep_ns__scheduled); *sleep_state->user_state_pointer =3D NULL; - timer_del(&sleep_state->ts); aio_co_wake(sleep_state->co); } } @@ -49,6 +47,7 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType= type, int64_t ns, QemuCoSleepState **sleep_state) { AioContext *ctx =3D qemu_get_current_aio_context(); + QEMUTimer ts; QemuCoSleepState state =3D { .co =3D qemu_coroutine_self(), .user_state_pointer =3D sleep_state, @@ -63,10 +62,11 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockTy= pe type, int64_t ns, abort(); } =20 - aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_stat= e); + aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state); *sleep_state =3D &state; - timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); + timer_mod(&ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); + timer_del(&ts); =20 /* qemu_co_sleep_wake clears *sleep_state before resuming this corouti= ne. */ assert(*sleep_state =3D=3D NULL); --=20 2.31.1 From nobody Tue May 14 15:57:53 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1621246099; cv=none; d=zohomail.com; s=zohoarc; b=LS18OG985sIzrECFiXJYrYtlzwHglR9YsURxW8lyfwniokv9fXsa+49vxLWPCdVRM5j+gWXTNwuPoPilzd5vMS7YudGxIZ4mJ47kWtrYrAQ36iuP58UKAKfdJQqRCM1a9qRbfeyWQgS2zlhVNYDfuFO3vSnwK26/FAC8epUQQNI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621246099; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9HgVN9JuTcPWGk6l9eQ7hl/THl658HFq7ITeS5EdxUw=; b=GQVjawnU9cRZa3cJwOJWCA5idURca/k+k1LPsS2HzEOxQPCoV0jIdPUz2H17DqmGLpRMhQXGGRYYfYXJZCpSu+fEuc9bbzzVmYRRADw8tbtSXYRbHkdwCiysCbXYr5RBMeCWiTyU1z2xAL3XfquFpAwqNTmM7xP3BfKJL7G0yiQ= ARC-Authentication-Results: i=1; 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 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 1621246099375641.1044647871228; Mon, 17 May 2021 03:08:19 -0700 (PDT) Received: from localhost ([::1]:51744 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liaAc-0007Us-AP for importer@patchew.org; Mon, 17 May 2021 06:08:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43050) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lia8O-0004pj-0i; Mon, 17 May 2021 06:06:00 -0400 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]:34496) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lia8L-0001h0-5D; Mon, 17 May 2021 06:05:59 -0400 Received: by mail-ed1-x534.google.com with SMTP id l7so6169865edb.1; Mon, 17 May 2021 03:05:56 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id h9sm10659926ede.93.2021.05.17.03.05.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 03:05:54 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=9HgVN9JuTcPWGk6l9eQ7hl/THl658HFq7ITeS5EdxUw=; b=ZiNhWEvI/pP8FsWtcGpigj8iocqMUfwtz5BPkd5+Ja4vrwzcFoUw0p3GW0kLl0cavV Jam00zkqYN7kZaSKr7uZwkq+J6MjOzYGacuc0BqIO0s830hMdCaAr0RtqimYQ21L1hge BIKY9z540szSfcrd5jFSscZCe+uvKCCV9SDdrhLrPLTYj0Q7AM1F8DmYa03HaQ6CmSw+ S5TGdKuHyH35UemdrE8Fiw0aBn+0WLefcH47uCQ7tGYglUQzhcyUPt65PCZeJJDVEmu8 54trXvbtxiGrPVKunZB17qcJb5WHsqVOezFvdMeraTk9X43BWAMgh5owXNGgk+4Qqt43 DqqA== 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:mime-version:content-transfer-encoding; bh=9HgVN9JuTcPWGk6l9eQ7hl/THl658HFq7ITeS5EdxUw=; b=cLoCYU/N/vZ6aiSyyT0eBUyvjPTsmhYvd12O59Gehc6vc0fWES6KMMGy3SNNGwKKrZ 69/d4FuSuwgMuhTYvBC7zmPUghLQWwfa0Ugvtll74nYaMwyJvrhbeZpRsFMCOuRJu2wg 23/AqFOXuyiTHZ8cKPcr6XenOWt7P8H9jqbdwdqO9hWLmhTFO2HOGFk6+f2kP9wNygh8 rBLHDT7HGSIixIkDEH5yIL4URe/oTPidJOV9RKQbTTJv9Qv3jac9D1MhKm60yB+9jwOW Y1QQ5HVTw4JW2s/1ryBLQidb8a0hDHeiRAyBqpx73i/wXvAUgHY1dWj+W4R6uCOSoapW 6QRQ== X-Gm-Message-State: AOAM532EZ9DAxtrJ9XSN+JBnf4WdD4qgmHkxygYnVg8bzTKjcwb+vrmy 2E/DjjlGGBostSMgXcBHohQD3V2EYvIjVA== X-Google-Smtp-Source: ABdhPJyjn2ZpzTIeH1HxLr5H1py+Z8GZirhgvAm05ucNA1PL87KypNMEEx/y19t8/DXeHrOb0IGypg== X-Received: by 2002:a05:6402:22e8:: with SMTP id dn8mr27681120edb.191.1621245955119; Mon, 17 May 2021 03:05:55 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH v2 5/6] coroutine-sleep: replace QemuCoSleepState pointer with struct in the API Date: Mon, 17 May 2021 12:05:47 +0200 Message-Id: <20210517100548.28806-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517100548.28806-1-pbonzini@redhat.com> References: <20210517100548.28806-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::534; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x534.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: eesposit@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com, qemu-block@nongnu.org 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" Right now, users of qemu_co_sleep_ns_wakeable are simply passing a pointer to QemuCoSleepState by reference to the function. But QemuCoSleepState really is just a Coroutine*; making the content of the struct public is just as efficient and lets us skip the user_state_pointer indirection. Since the usage is changed, take the occasion to rename the struct to QemuCoSleep. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini --- block/block-copy.c | 8 ++++---- block/nbd.c | 10 ++++----- include/qemu/coroutine.h | 23 +++++++++++---------- util/qemu-coroutine-sleep.c | 41 ++++++++++++++++--------------------- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index f896dc56f2..c2e5090412 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -50,7 +50,7 @@ typedef struct BlockCopyCallState { /* State */ int ret; bool finished; - QemuCoSleepState *sleep_state; + QemuCoSleep sleep; bool cancelled; =20 /* OUT parameters */ @@ -625,8 +625,8 @@ block_copy_dirty_clusters(BlockCopyCallState *call_stat= e) if (ns > 0) { block_copy_task_end(task, -EAGAIN); g_free(task); - qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, ns, - &call_state->sleep_state); + qemu_co_sleep_ns_wakeable(&call_state->sleep, + QEMU_CLOCK_REALTIME, ns); continue; } } @@ -674,7 +674,7 @@ out: =20 void block_copy_kick(BlockCopyCallState *call_state) { - qemu_co_sleep_wake(call_state->sleep_state); + qemu_co_sleep_wake(&call_state->sleep); } =20 /* diff --git a/block/nbd.c b/block/nbd.c index 1c6315b168..616f9ae6c4 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -116,7 +116,7 @@ typedef struct BDRVNBDState { CoQueue free_sema; Coroutine *connection_co; Coroutine *teardown_co; - QemuCoSleepState *connection_co_sleep_ns_state; + QemuCoSleep reconnect_sleep; bool drained; bool wait_drained_end; int in_flight; @@ -289,7 +289,7 @@ static void coroutine_fn nbd_client_co_drain_begin(Bloc= kDriverState *bs) BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; =20 s->drained =3D true; - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); + qemu_co_sleep_wake(&s->reconnect_sleep); =20 nbd_co_establish_connection_cancel(bs, false); =20 @@ -328,7 +328,7 @@ static void nbd_teardown_connection(BlockDriverState *b= s) =20 s->state =3D NBD_CLIENT_QUIT; if (s->connection_co) { - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); + qemu_co_sleep_wake(&s->reconnect_sleep); nbd_co_establish_connection_cancel(bs, true); } if (qemu_in_coroutine()) { @@ -685,8 +685,8 @@ static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDS= tate *s) } bdrv_inc_in_flight(s->bs); } else { - qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout, - &s->connection_co_sleep_ns_state); + qemu_co_sleep_ns_wakeable(&s->reconnect_sleep, + QEMU_CLOCK_REALTIME, timeout); if (s->drained) { continue; } diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index c5d7742989..82c0671f80 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -291,21 +291,22 @@ void qemu_co_rwlock_wrlock(CoRwlock *lock); */ void qemu_co_rwlock_unlock(CoRwlock *lock); =20 -typedef struct QemuCoSleepState QemuCoSleepState; +typedef struct QemuCoSleep { + Coroutine *to_wake; +} QemuCoSleep; =20 /** - * Yield the coroutine for a given duration. During this yield, @sleep_sta= te - * is set to an opaque pointer, which may be used for - * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when = the - * timer fires. Don't save the obtained value to other variables and don't= call - * qemu_co_sleep_wake from another aio context. + * Yield the coroutine for a given duration. Initializes @w so that, + * during this yield, it can be passed to qemu_co_sleep_wake() to + * terminate the sleep. */ -void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, - QemuCoSleepState **sleep_state= ); +void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, + QEMUClockType type, int64_t ns= ); + static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64= _t ns) { - QemuCoSleepState *unused =3D NULL; - qemu_co_sleep_ns_wakeable(type, ns, &unused); + QemuCoSleep w =3D { 0 }; + qemu_co_sleep_ns_wakeable(&w, type, ns); } =20 /** @@ -314,7 +315,7 @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUCl= ockType type, int64_t ns) * qemu_co_sleep_ns() and should be checked to be non-NULL before calling * qemu_co_sleep_wake(). */ -void qemu_co_sleep_wake(QemuCoSleepState *sleep_state); +void qemu_co_sleep_wake(QemuCoSleep *w); =20 /** * Yield until a file descriptor becomes readable diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 1d25019620..89c3b758c5 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -19,42 +19,37 @@ =20 static const char *qemu_co_sleep_ns__scheduled =3D "qemu_co_sleep_ns"; =20 -struct QemuCoSleepState { +void qemu_co_sleep_wake(QemuCoSleep *w) +{ Coroutine *co; - QemuCoSleepState **user_state_pointer; -}; =20 -void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) -{ - if (sleep_state) { + co =3D w->to_wake; + w->to_wake =3D NULL; + if (co) { /* Write of schedule protected by barrier write in aio_co_schedule= */ - const char *scheduled =3D qatomic_cmpxchg(&sleep_state->co->schedu= led, + const char *scheduled =3D qatomic_cmpxchg(&co->scheduled, qemu_co_sleep_ns__schedule= d, NULL); =20 assert(scheduled =3D=3D qemu_co_sleep_ns__scheduled); - *sleep_state->user_state_pointer =3D NULL; - aio_co_wake(sleep_state->co); + aio_co_wake(co); } } =20 static void co_sleep_cb(void *opaque) { - QemuCoSleepState **sleep_state =3D opaque; - qemu_co_sleep_wake(*sleep_state); + QemuCoSleep *w =3D opaque; + qemu_co_sleep_wake(w); } =20 -void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, - QemuCoSleepState **sleep_state) +void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, + QEMUClockType type, int64_t ns) { + Coroutine *co =3D qemu_coroutine_self(); AioContext *ctx =3D qemu_get_current_aio_context(); QEMUTimer ts; - QemuCoSleepState state =3D { - .co =3D qemu_coroutine_self(), - .user_state_pointer =3D sleep_state, - }; =20 - const char *scheduled =3D qatomic_cmpxchg(&state.co->scheduled, NULL, - qemu_co_sleep_ns__scheduled); + const char *scheduled =3D qatomic_cmpxchg(&co->scheduled, NULL, + qemu_co_sleep_ns__scheduled); if (scheduled) { fprintf(stderr, "%s: Co-routine was already scheduled in '%s'\n", @@ -62,12 +57,12 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockTy= pe type, int64_t ns, abort(); } =20 - aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state); - *sleep_state =3D &state; + w->to_wake =3D co; + aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w), timer_mod(&ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); timer_del(&ts); =20 - /* qemu_co_sleep_wake clears *sleep_state before resuming this corouti= ne. */ - assert(*sleep_state =3D=3D NULL); + /* w->to_wake is cleared before resuming this coroutine. */ + assert(w->to_wake =3D=3D NULL); } --=20 2.31.1 From nobody Tue May 14 15:57:53 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1621247064; cv=none; d=zohomail.com; s=zohoarc; b=fgm4HOnaPsmF+JY8KgRmm87qnBvZ6NTbwbPPk3TGm7E+XKfHMdKNH43jlYIboDqLrRIe64QYAiwHhAHQJl+uyMHyog2er2TQ20wrRyZI7TmGF4MoiIfTjGFv8yBFffv/ftyU815QhWcriX3ynnIaQv4/aUlQ7w2E5fWNbYASuaw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621247064; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UzkfuYorD23EZdVdpax3DBoMJaYVD6QjEIBFy4F93kI=; b=EUjXGayz+825bx7HkqMtmtxmkWRKHkbu8+S77jgQnsaJiNfQCj11d+7QS9i7ngOh8w5U2TGPamOKt2rsfvzb45gd+cF0wMa/uGQczHLncIoUZI4buCtq7/EWnusLBpb2IIev+JobtH8PYMXiWpfRZKEiGRC0zgFlArClwiVHL+8= ARC-Authentication-Results: i=1; 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 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 1621247064152510.2531377948575; Mon, 17 May 2021 03:24:24 -0700 (PDT) Received: from localhost ([::1]:56664 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liaQB-00053g-4U for importer@patchew.org; Mon, 17 May 2021 06:24:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43060) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lia8O-0004qo-LH; Mon, 17 May 2021 06:06:01 -0400 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]:45785) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lia8L-0001hT-UT; Mon, 17 May 2021 06:05:59 -0400 Received: by mail-ed1-x531.google.com with SMTP id a25so6122697edr.12; Mon, 17 May 2021 03:05:56 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id h9sm10659926ede.93.2021.05.17.03.05.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 03:05:55 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=UzkfuYorD23EZdVdpax3DBoMJaYVD6QjEIBFy4F93kI=; b=Jmp6Jy4teyHM9FSPnKiu1JMU/BFXWKoGOK7vPkQC41jbb2oQDyXt1nEQdgwhrGl+/6 Zvw4cnTgJV+91hEG7tr9MkvHctd0J5uLdb51B756XpJtXPZHK+2ib+AVyKa9WayRQPyL QEl5+I4t1TvOAGwPOHaw+uMeVvsyRnvy22kEdkW+BGXVUCGkqZzbWhBebTIUOAUZ/244 CNz10j0nvKC1EwY75RASH43kO41ts1efMwwWr/DMcpTkUkA7Pvmmp85Cjv4yo/gu7F8w MNAhTfqEZ7a7tyDXo7wa9n+3PNEH0unsZJQ3YiDxcAmE/y6p1w9rUHid4CAcAsgaMEJc cxGQ== 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:mime-version:content-transfer-encoding; bh=UzkfuYorD23EZdVdpax3DBoMJaYVD6QjEIBFy4F93kI=; b=qYb3Bt8k3f21K7/0raY+0QvCRvF78CgnLddECbh5uo1C6SsEZiYCKp48gtuj8BwzJ1 B7mrQispLeMFODF4yOYnKXh3XOWekA92nVfsnO8nkR/1QoNtVdtbhw89UzW5yIwW9QYv F6uD6q9qMxAooo/22nCaS4kAPd7H06l0aKrZan3qqio7Ti2WdX1xtSMfk7lnFikpeA11 yjnmz69VaWznkByn8UJ4ciESlvcNc30+nBAAekmsvFgzLt330GdM7gYYUojWgKr4UbKH YE8exy6WbRWqDFLdfq1212OkW+xrXK9JQeFU2TXPjDh+x4RQpL1ccymRFod9mHvBpR1C QzcA== X-Gm-Message-State: AOAM532TJHFfAPyu5OfYX+5LUj6+8KfzjFTxT4+prX+b+T+GS50hLFEx YHVaHAdQz5xA+ZQlHEnKvsC2ZRRLgWZhUA== X-Google-Smtp-Source: ABdhPJxWsgmbkfRZBRbp//t5yMf49bb0QTavKZGxE89smb/e6SA7Lue4SUROsaTWrWjeAlKt3hi7uA== X-Received: by 2002:a05:6402:487:: with SMTP id k7mr40518859edv.315.1621245955770; Mon, 17 May 2021 03:05:55 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH v2 6/6] coroutine-sleep: introduce qemu_co_sleep Date: Mon, 17 May 2021 12:05:48 +0200 Message-Id: <20210517100548.28806-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517100548.28806-1-pbonzini@redhat.com> References: <20210517100548.28806-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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::531; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x531.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: eesposit@redhat.com, vsementsov@virtuozzo.com, stefanha@redhat.com, qemu-block@nongnu.org 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" Allow using QemuCoSleep to sleep forever until woken by qemu_co_sleep_wake. This makes the logic of qemu_co_sleep_ns_wakeable easy to understand. In the future we will introduce an API that can work even if the sleep and wake happen from different threads. For now, initializing w->to_wake after timer_mod is fine because the timer can only fire in the same AioContext. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini --- include/qemu/coroutine.h | 5 +++++ util/qemu-coroutine-sleep.c | 26 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 82c0671f80..292e61aef0 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -303,6 +303,11 @@ typedef struct QemuCoSleep { void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, QEMUClockType type, int64_t ns= ); =20 +/** + * Yield the coroutine until the next call to qemu_co_sleep_wake. + */ +void coroutine_fn qemu_co_sleep(QemuCoSleep *w); + static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64= _t ns) { QemuCoSleep w =3D { 0 }; diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 89c3b758c5..571ab521ff 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -41,12 +41,9 @@ static void co_sleep_cb(void *opaque) qemu_co_sleep_wake(w); } =20 -void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, - QEMUClockType type, int64_t ns) +void coroutine_fn qemu_co_sleep(QemuCoSleep *w) { Coroutine *co =3D qemu_coroutine_self(); - AioContext *ctx =3D qemu_get_current_aio_context(); - QEMUTimer ts; =20 const char *scheduled =3D qatomic_cmpxchg(&co->scheduled, NULL, qemu_co_sleep_ns__scheduled); @@ -58,11 +55,26 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep= *w, } =20 w->to_wake =3D co; - aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w), - timer_mod(&ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); - timer_del(&ts); =20 /* w->to_wake is cleared before resuming this coroutine. */ assert(w->to_wake =3D=3D NULL); } + +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; + + aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w); + timer_mod(&ts, qemu_clock_get_ns(type) + ns); + + /* + * 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); +} --=20 2.31.1