From nobody Mon May 6 16:19:11 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622527160; cv=none; d=zohomail.com; s=zohoarc; b=L59cOTZRIWjbvbwE0dzgrNV08BxZFK43zCf4lQsaAnnWHHkDevN6fwNWk90vJxKU6z2xW8B4pppy3t8xyEl5JROMX9WKC/rLNf1WLuoruhtDV71zd9eLKXH8QSgi+mE9U/+P7dJDGH/YCtbOJw6Ghwdd2yJ18dI8eVukeezKgGA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622527160; 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=qxYh9HqsIPi/lsW5JtdpMe4diqRXLiDe943ozkPv/Zw=; b=ID29lHv5S3HozCJhxf/r0Ul76uufcgwjVkcMY/I4hsfmbedMiZoYcFESv4lu11z6xqw5d/kYBr2RUzO8CTsuYPl+mPfSym4do0S4wTbruMajdTkS637S6wSIFQnusn6FwXQOm9RV8eNnT2tP6EepQ/HaGYPoNRPW0IPzYfzk1/s= 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+importer2=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 1622527160325620.2998574393854; Mon, 31 May 2021 22:59:20 -0700 (PDT) Received: from localhost ([::1]:60864 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lnxQt-0001nh-BG for importer2@patchew.org; Tue, 01 Jun 2021 01:59:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57814) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lnxPN-00086t-NX for qemu-devel@nongnu.org; Tue, 01 Jun 2021 01:57:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24846) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lnxPK-00021C-Ma for qemu-devel@nongnu.org; Tue, 01 Jun 2021 01:57:45 -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-440-VEwVkrWLMDi7MFnvjLR55Q-1; Tue, 01 Jun 2021 01:57:40 -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 B4D15501E0; Tue, 1 Jun 2021 05:57:39 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.33.36.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2C5569FA4; Tue, 1 Jun 2021 05:57:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622527062; 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=qxYh9HqsIPi/lsW5JtdpMe4diqRXLiDe943ozkPv/Zw=; b=bEixVPbJQf7SpymxVwr9HuuxD3TWWJJgcnPZA2lvalggTs8JH6WtFaP3TEsK2pGeLNAclt lWz1NHSK3IF83WaVn/j9P+FMZrQg7ionuf51psnAd3Jo3B5zhgSaLYmD6qBkwvHTYdB8Od m7XdyacIxig8yux0vNjeNTZeSlDDjak= X-MC-Unique: VEwVkrWLMDi7MFnvjLR55Q-1 From: Sergio Lopez To: qemu-devel@nongnu.org Subject: [PATCH 1/2] block-backend: add drained_poll Date: Tue, 1 Jun 2021 07:57:27 +0200 Message-Id: <20210601055728.90849-2-slp@redhat.com> In-Reply-To: <20210601055728.90849-1-slp@redhat.com> References: <20210601055728.90849-1-slp@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=slp@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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=slp@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.372, 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: Kevin Wolf , Vladimir Sementsov-Ogievskiy , Sergio Lopez , qemu-block@nongnu.org, Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Allow block backends to poll their devices/users to check if they have been quiesced when entering a drained section. This will be used in the next patch to wait for the NBD server to be completely quiesced. Suggested-by: Kevin Wolf Signed-off-by: Sergio Lopez Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- block/block-backend.c | 7 ++++++- include/sysemu/block-backend.h | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/block/block-backend.c b/block/block-backend.c index de5496af66..163ca05b97 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -2393,8 +2393,13 @@ static void blk_root_drained_begin(BdrvChild *child) static bool blk_root_drained_poll(BdrvChild *child) { BlockBackend *blk =3D child->opaque; + int ret =3D 0; assert(blk->quiesce_counter); - return !!blk->in_flight; + + if (blk->dev_ops && blk->dev_ops->drained_poll) { + ret =3D blk->dev_ops->drained_poll(blk->dev_opaque); + } + return ret || !!blk->in_flight; } =20 static void blk_root_drained_end(BdrvChild *child, int *drained_end_counte= r) diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index 880e903293..9992072e18 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -66,6 +66,10 @@ typedef struct BlockDevOps { * Runs when the backend's last drain request ends. */ void (*drained_end)(void *opaque); + /* + * Is the device drained? + */ + bool (*drained_poll)(void *opaque); } BlockDevOps; =20 /* This struct is embedded in (the private) BlockBackend struct and contai= ns --=20 2.26.2 From nobody Mon May 6 16:19:11 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1622527257; cv=none; d=zohomail.com; s=zohoarc; b=WT9S5jMnXmPN/dNRVK1QQ1I7/YyG41GXtWrmMUNh73ZxFpBpifsy+MG4JiIPYbwjZoG5KZbpird9tYrizDG+t2vdEDUnBkG7LsgZTSd6mHT/PmVA2MIu5OIEWxJVAB96yFwOgb9POjo1hl8uiFStblwJiy3lbd7Ox9AmB/14owQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622527257; 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=/O6nN7HyWGO9hGQpiIgOSJ/l5K5+rWXv4rWS9qdlYJI=; b=jWUTdNWkHNZM9blkpetD9yqq8S5B1q2EZqfk+N8e6LyvBLWQxlNxEhMmxQRrEGcfniKASmB64HLmIjxIWi+Lqz/1ojCUbxR9cKISc1PlIUTpeVX3WxCRGYGmJ+kMR6A7MAgNsm6ssMO1q/6n8Ww3XN+9V01u7racu43+U6Ob7WM= 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+importer2=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 1622527256649569.9132141675876; Mon, 31 May 2021 23:00:56 -0700 (PDT) Received: from localhost ([::1]:36692 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lnxSQ-0004YG-N6 for importer2@patchew.org; Tue, 01 Jun 2021 02:00:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57840) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lnxPP-0008Ae-L8 for qemu-devel@nongnu.org; Tue, 01 Jun 2021 01:57:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:22264) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lnxPN-00023i-Ex for qemu-devel@nongnu.org; Tue, 01 Jun 2021 01:57:47 -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-443-rXbLyQ_aPX--9izAQt0OTA-1; Tue, 01 Jun 2021 01:57:42 -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 C82D5800D55; Tue, 1 Jun 2021 05:57:41 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.33.36.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1208369FA4; Tue, 1 Jun 2021 05:57:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622527064; 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=/O6nN7HyWGO9hGQpiIgOSJ/l5K5+rWXv4rWS9qdlYJI=; b=YcHMISx7fSfJbgjv+aETbkfqVgMiBH5WIGZiuxAl/ee5bI+mLQE9wUkL3y40EQ1fx6ISPx 6aqk292vXNnc66jzxu8lVmg6DEW/7NngTqM3QOe89aH1k4Pe+t40uQxHREkJ8+SsQQDf9p 7/Ngy1DuyfkWPPPx8Lw8r4u0PmSqvck= X-MC-Unique: rXbLyQ_aPX--9izAQt0OTA-1 From: Sergio Lopez To: qemu-devel@nongnu.org Subject: [PATCH 2/2] nbd/server: Use drained block ops to quiesce the server Date: Tue, 1 Jun 2021 07:57:28 +0200 Message-Id: <20210601055728.90849-3-slp@redhat.com> In-Reply-To: <20210601055728.90849-1-slp@redhat.com> References: <20210601055728.90849-1-slp@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=slp@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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=slp@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.372, 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: Kevin Wolf , Vladimir Sementsov-Ogievskiy , Sergio Lopez , qemu-block@nongnu.org, Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" 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 to be 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 --- nbd/server.c | 99 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 86a44a9b41..33e55479d7 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -132,7 +132,7 @@ struct NBDClient { CoMutex send_lock; Coroutine *send_coroutine; =20 - bool read_yielding; + GSList *yield_co_list; /* List of coroutines yielding on nbd_read_eof = */ bool quiescing; =20 QTAILQ_ENTRY(NBDClient) next; @@ -1367,6 +1367,7 @@ static inline int coroutine_fn nbd_read_eof(NBDClient *client, void *buffer, size_t size, Error **errp) { bool partial =3D false; + Coroutine *co; =20 assert(size); while (size > 0) { @@ -1375,9 +1376,12 @@ nbd_read_eof(NBDClient *client, void *buffer, size_t= size, Error **errp) =20 len =3D qio_channel_readv(client->ioc, &iov, 1, errp); if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { - client->read_yielding =3D true; + co =3D qemu_coroutine_self(); + + client->yield_co_list =3D g_slist_prepend(client->yield_co_lis= t, co); qio_channel_yield(client->ioc, G_IO_IN); - client->read_yielding =3D false; + client->yield_co_list =3D g_slist_remove(client->yield_co_list= , co); + if (client->quiescing) { return -EAGAIN; } @@ -1513,6 +1517,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 +1539,75 @@ 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; + Coroutine *co; + GSList *entry; + GSList *coroutine_list; =20 - trace_nbd_blk_aio_detach(exp->name, exp->common.ctx); + QTAILQ_FOREACH(client, &exp->clients, next) { + if (client->nb_requests !=3D 0) { + /* + * Enter coroutines waiting for new requests on nbd_read_eof()= , so + * we don't depend on the client to wake us up. + */ + coroutine_list =3D g_slist_copy(client->yield_co_list); + for (entry =3D coroutine_list; + entry !=3D NULL; + entry =3D g_slist_next(entry)) { + co =3D entry->data; + qemu_aio_coroutine_enter(exp->common.ctx, co); + } + g_slist_free(coroutine_list); =20 - aio_wait_bh_oneshot(exp->common.ctx, nbd_aio_detach_bh, exp); + return 1; + } + } =20 - exp->common.ctx =3D NULL; + return 0; } =20 static void nbd_eject_notifier(Notifier *n, void *data) @@ -1594,6 +1629,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 +1756,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 +1838,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.26.2