From nobody Tue Apr 15 08:22:26 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622642457; cv=none; d=zohomail.com; s=zohoarc; b=IcP5ALdB8zXX/I0qurn6or2kDZRhVgD6EVsAyIbid4v41k/lxx+UuCnlHsXHfIPN/+VzUv3ceG8opQjyy3iixMiSic6ElzaTlllLzcCgqcIRSNXpdz76U5Hkerrix9sn94ytnK0JyFEr7FuytgvVrfkyFxvgVZQB1C4CDiF7nys= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622642457; h=Content-Type: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=kLFHWJbTkHtI9zWWTquem6lLwku6QK5+Q5XDeWp4x3Q=; b=LejwfZAmfyRfj1rCgiUNni7CXTpiYtTwIWIBUs9bcZxBhK12OzITYELttZ+s7tOfWHWKt1jYbsYXpZu1K9An7j1TTBgm5Yz59F1rfBLoHals+p6Yh/+OfUoS9hguQGff5W3EfnYjzuz+ZJT/TbVVEnarCEAOuslW6TvxxxMVB3U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass 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 1622642457361601.5195089094117; Wed, 2 Jun 2021 07:00:57 -0700 (PDT) Received: from localhost ([::1]:34520 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1loRQW-0001S5-NI for importer@patchew.org; Wed, 02 Jun 2021 10:00:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44190) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1loRCg-0002Yn-II for qemu-devel@nongnu.org; Wed, 02 Jun 2021 09:46:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32895) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1loRCe-0007Uo-7u for qemu-devel@nongnu.org; Wed, 02 Jun 2021 09:46:38 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-429-FsgVFW8gMNSAJERj63XaYg-1; Wed, 02 Jun 2021 09:46:33 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D17D191DB7E; Wed, 2 Jun 2021 13:45:55 +0000 (UTC) Received: from merkur.redhat.com (ovpn-114-240.ams2.redhat.com [10.36.114.240]) by smtp.corp.redhat.com (Postfix) with ESMTP id DC67560BD9; Wed, 2 Jun 2021 13:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622641595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kLFHWJbTkHtI9zWWTquem6lLwku6QK5+Q5XDeWp4x3Q=; b=ZOojZxJjJ2S+ZdsX4XapcjPKvtvwYwwpUz2+7Mc6YGFmNpf1Pz5v3aoDw2WTkSRYn9m0rE aGFLtAjKlgpS+22g8plkdoC6OIuvMZaiZ9tX6HjeRWstlwrZDsQD1R4a2Vr2/tithrWFRu xZ1pssCm4YpoXyir8OwvcvetUOonHPc= X-MC-Unique: FsgVFW8gMNSAJERj63XaYg-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PULL 17/20] nbd/server: Use drained block ops to quiesce the server Date: Wed, 2 Jun 2021 15:45:26 +0200 Message-Id: <20210602134529.231756-18-kwolf@redhat.com> In-Reply-To: <20210602134529.231756-1-kwolf@redhat.com> References: <20210602134529.231756-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kwolf@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=216.205.24.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.371, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Sergio Lopez Before switching between AioContexts we need to make sure that we're fully quiesced ("nb_requests =3D=3D 0" for every client) when entering the drained section. To do this, we set "quiescing =3D true" for every client on ".drained_begin" to prevent new coroutines from being created, and check if "nb_requests =3D=3D 0" on ".drained_poll". Finally, once we're exiting the drained section, on ".drained_end" we set "quiescing =3D false" and call "nbd_client_receive_next_request()" to resume the processing of new requests. With these changes, "blk_aio_attach()" and "blk_aio_detach()" can be reverted to be as simple as they were before f148ae7d36. RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=3D1960137 Suggested-by: Kevin Wolf Signed-off-by: Sergio Lopez Message-Id: <20210602060552.17433-3-slp@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Kevin Wolf --- nbd/server.c | 82 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 86a44a9b41..b60ebc3ab6 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1513,6 +1513,11 @@ static void nbd_request_put(NBDRequestData *req) g_free(req); =20 client->nb_requests--; + + if (client->quiescing && client->nb_requests =3D=3D 0) { + aio_wait_kick(); + } + nbd_client_receive_next_request(client); =20 nbd_client_put(client); @@ -1530,49 +1535,68 @@ static void blk_aio_attached(AioContext *ctx, void = *opaque) QTAILQ_FOREACH(client, &exp->clients, next) { qio_channel_attach_aio_context(client->ioc, ctx); =20 + assert(client->nb_requests =3D=3D 0); assert(client->recv_coroutine =3D=3D NULL); assert(client->send_coroutine =3D=3D NULL); - - if (client->quiescing) { - client->quiescing =3D false; - nbd_client_receive_next_request(client); - } } } =20 -static void nbd_aio_detach_bh(void *opaque) +static void blk_aio_detach(void *opaque) { NBDExport *exp =3D opaque; NBDClient *client; =20 + trace_nbd_blk_aio_detach(exp->name, exp->common.ctx); + QTAILQ_FOREACH(client, &exp->clients, next) { qio_channel_detach_aio_context(client->ioc); + } + + exp->common.ctx =3D NULL; +} + +static void nbd_drained_begin(void *opaque) +{ + NBDExport *exp =3D opaque; + NBDClient *client; + + QTAILQ_FOREACH(client, &exp->clients, next) { client->quiescing =3D true; + } +} =20 - if (client->recv_coroutine) { - if (client->read_yielding) { - qemu_aio_coroutine_enter(exp->common.ctx, - client->recv_coroutine); - } else { - AIO_WAIT_WHILE(exp->common.ctx, client->recv_coroutine != =3D NULL); - } - } +static void nbd_drained_end(void *opaque) +{ + NBDExport *exp =3D opaque; + NBDClient *client; =20 - if (client->send_coroutine) { - AIO_WAIT_WHILE(exp->common.ctx, client->send_coroutine !=3D NU= LL); - } + QTAILQ_FOREACH(client, &exp->clients, next) { + client->quiescing =3D false; + nbd_client_receive_next_request(client); } } =20 -static void blk_aio_detach(void *opaque) +static bool nbd_drained_poll(void *opaque) { NBDExport *exp =3D opaque; + NBDClient *client; =20 - trace_nbd_blk_aio_detach(exp->name, exp->common.ctx); + QTAILQ_FOREACH(client, &exp->clients, next) { + if (client->nb_requests !=3D 0) { + /* + * If there's a coroutine waiting for a request on nbd_read_eo= f() + * enter it here so we don't depend on the client to wake it u= p. + */ + if (client->recv_coroutine !=3D NULL && client->read_yielding)= { + qemu_aio_coroutine_enter(exp->common.ctx, + client->recv_coroutine); + } =20 - aio_wait_bh_oneshot(exp->common.ctx, nbd_aio_detach_bh, exp); + return true; + } + } =20 - exp->common.ctx =3D NULL; + return false; } =20 static void nbd_eject_notifier(Notifier *n, void *data) @@ -1594,6 +1618,12 @@ void nbd_export_set_on_eject_blk(BlockExport *exp, B= lockBackend *blk) blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier); } =20 +static const BlockDevOps nbd_block_ops =3D { + .drained_begin =3D nbd_drained_begin, + .drained_end =3D nbd_drained_end, + .drained_poll =3D nbd_drained_poll, +}; + static int nbd_export_create(BlockExport *blk_exp, BlockExportOptions *exp= _args, Error **errp) { @@ -1715,8 +1745,17 @@ static int nbd_export_create(BlockExport *blk_exp, B= lockExportOptions *exp_args, =20 exp->allocation_depth =3D arg->allocation_depth; =20 + /* + * We need to inhibit request queuing in the block layer to ensure we = can + * be properly quiesced when entering a drained section, as our corout= ines + * servicing pending requests might enter blk_pread(). + */ + blk_set_disable_request_queuing(blk, true); + blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, ex= p); =20 + blk_set_dev_ops(blk, &nbd_block_ops, exp); + QTAILQ_INSERT_TAIL(&exports, exp, next); =20 return 0; @@ -1788,6 +1827,7 @@ static void nbd_export_delete(BlockExport *blk_exp) } blk_remove_aio_context_notifier(exp->common.blk, blk_aio_attached, blk_aio_detach, exp); + blk_set_disable_request_queuing(exp->common.blk, false); } =20 for (i =3D 0; i < exp->nr_export_bitmaps; i++) { --=20 2.30.2