From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 275CAC433ED for ; Mon, 3 May 2021 11:30:19 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 795D561177 for ; Mon, 3 May 2021 11:30:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 795D561177 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:47248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ldWmH-0003aX-A8 for qemu-devel@archiver.kernel.org; Mon, 03 May 2021 07:30:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41000) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldWi6-0008Ks-Tt for qemu-devel@nongnu.org; Mon, 03 May 2021 07:25:58 -0400 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]:43737) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ldWi5-0003Xv-AB for qemu-devel@nongnu.org; Mon, 03 May 2021 07:25:58 -0400 Received: by mail-ed1-x530.google.com with SMTP id e7so5815554edu.10 for ; Mon, 03 May 2021 04:25:56 -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=KlDK80YilGQbH9G5IoPdIukwRost8JFR5BmMZD0EMTE=; b=vbI2V7KLVpVA3Z5mSZOHO/6riMWZFW/hpNb+K96MSAVF4Le4ZzE3docY+pUiJ176Hd x/c/42Kv2uoCBHKop/wRWcYqE/TSghrPu5oTjt152w+BiZt9LawyH9FDW5cqy6dPanNV PjpCJprO3/ICCbciUq3O57BvzD8el01RGeDtj6Hflfn168RALJ1AyqhYesTI1V+yPrDb 5P3BJ7Rd+3f4cfa11DlWctoM8SqCwhwrzRRK+vktZg4OWbuLmP28JTeBm9gv2RgP5dKd 5CT2UTboH7jjITvxKdpQ1H2NI6PzYj+2FVmBOHPbyh/KIFJIki/o8d0vkob26QvpFCHV TziQ== 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=KlDK80YilGQbH9G5IoPdIukwRost8JFR5BmMZD0EMTE=; b=jUlxYeoKUGAB2qdvClvUGvWSCyxH8k7ziFjlzgUyZLC9mWZJG8XvpH3aEpXdn5k+if 3Rh2NjNb2OYdq9SXwX7yHlYuNyBP4hFxgbOCfO47gL8RKPWEExMKcFBvbu3CqlWF0gRc YadIRBes0oGsk6mLgr+3owwIXFlwKe2Ia8JyyJvyBeaJdeZsttAsj0VqOwqfhTi+TDTw FYG2d+Ul/6xiZ51S7kT6hjbAm9pGkO40f4YFTUPqLwywLdiMyKCzjKMehHZ8zRpo41cP rv3pa0/fjxpNu6R69GhqfvknxD+lIsKKBvV3sk4MJWjFI/554HMAtCgtg0YRdEdB5VGx 2RJw== X-Gm-Message-State: AOAM531rv/Lw4Y1EDqcPnaswzpWvbHbA/CKr9nAS3Bu/MoMIlecu1rUe 6tF/FfHXkJr2zUn7GQk7Zy7spurWM+c= X-Google-Smtp-Source: ABdhPJylOI75404DBqoEyLb9E7UYc5Sk196DMUJiAKARvLL32IUY0Kgclyc12sqI6CtblpOsBT7oSQ== X-Received: by 2002:a05:6402:17fc:: with SMTP id t28mr19534467edy.283.1620041155894; Mon, 03 May 2021 04:25:55 -0700 (PDT) Received: from localhost.localdomain ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id gn36sm2981317ejc.23.2021.05.03.04.25.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 May 2021 04:25:55 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 3/6] coroutine-sleep: allow qemu_co_sleep_wake that wakes nothing Date: Mon, 3 May 2021 13:25:47 +0200 Message-Id: <20210503112550.478521-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210503112550.478521-1-pbonzini@redhat.com> References: <20210503112550.478521-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, 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, stefanha@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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. 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: 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); } /* 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(BlockDriverState *bs) BDRVNBDState *s = (BDRVNBDState *)bs->opaque; s->drained = 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); nbd_co_establish_connection_cancel(bs, false); @@ -330,9 +328,7 @@ static void nbd_teardown_connection(BlockDriverState *bs) s->state = 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..096eea3444 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -27,19 +27,22 @@ struct QemuCoSleepState { void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) { - /* Write of schedule protected by barrier write in aio_co_schedule */ - const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled, - qemu_co_sleep_ns__scheduled, NULL); + if (sleep_state) { + /* Write of schedule protected by barrier write in aio_co_schedule */ + const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled, + qemu_co_sleep_ns__scheduled, NULL); - assert(scheduled == qemu_co_sleep_ns__scheduled); - *sleep_state->user_state_pointer = NULL; - timer_del(&sleep_state->ts); - aio_co_wake(sleep_state->co); + assert(scheduled == qemu_co_sleep_ns__scheduled); + *sleep_state->user_state_pointer = NULL; + timer_del(&sleep_state->ts); + aio_co_wake(sleep_state->co); + } } static void co_sleep_cb(void *opaque) { - qemu_co_sleep_wake(opaque); + QemuCoSleepState **sleep_state = opaque; + qemu_co_sleep_wake(*sleep_state); } 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(); } - 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_state); *sleep_state = &state; timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); -- 2.31.1