From nobody Mon Apr 14 13:52:14 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=1710767123; cv=none; d=zohomail.com; s=zohoarc; b=cNqUeA6C9uj66HGOufUBD8XWxTw20RrfCF9pUgqYBhQzc4REfditQOiPUpDg7GXe0j80lhccRkvSu9mAbrDvnyHaiqxOb3JYmKag407uN+KdmvciCjI9II0d7FNfyvPbRfuiicSK0lFQtik49raFOHZEuHT8GY5eJgEcInR8wF8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710767123; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VMgn9wPdUVxdSNXAYOjNfBipUlAU7SOC3OtijJaKeCI=; b=HRyGaY2c1dok5bf/5jdaUfT3h8STTD4ZlT4iJZh++EHc/x87MeHPjnlLaIjnZfY5dH/wMPhpg8sxjn2NkZxKgMntnP8SuuwArpYP/uGMCZS9kB6RfhfBgyyTqPsEgIU+sfreGY7XtmFxKsP67OKoDq6R8SW8yrUb9ey814WkaH8= 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) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1710767123511288.3117704739085; Mon, 18 Mar 2024 06:05:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rmCen-00069u-TI; Mon, 18 Mar 2024 09:04:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmCcd-0002Rl-0R for qemu-devel@nongnu.org; Mon, 18 Mar 2024 09:01:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmCcY-0007ei-D2 for qemu-devel@nongnu.org; Mon, 18 Mar 2024 09:01:45 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-484-liX711enNoavPHI5w0keKg-1; Mon, 18 Mar 2024 09:01:27 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A6B7A101A523; Mon, 18 Mar 2024 13:01:27 +0000 (UTC) Received: from merkur.redhat.com (unknown [10.39.194.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 19DD92166AE1; Mon, 18 Mar 2024 13:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710766889; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VMgn9wPdUVxdSNXAYOjNfBipUlAU7SOC3OtijJaKeCI=; b=ChJZPiygsW2r6eC78YgRQnomigTQ+UwSYX3AeIX1xjbQrhkHrt2985Va1KB55rj5THl2V8 xMLEy3W/Ld+3RnquAvVmV/+a1kdnecFHfC9lYmMVOdeeGwTohjsEKIgS7zjBcD2ykwXVAX 7qeNeCU8mlafZWXwaTXHiXFH1ydQ/WY= X-MC-Unique: liX711enNoavPHI5w0keKg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 02/15] nbd/server: Fix race in draining the export Date: Mon, 18 Mar 2024 14:01:05 +0100 Message-ID: <20240318130118.358920-3-kwolf@redhat.com> In-Reply-To: <20240318130118.358920-1-kwolf@redhat.com> References: <20240318130118.358920-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 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=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1710767124615100003 Content-Type: text/plain; charset="utf-8" When draining an NBD export, nbd_drained_begin() first sets client->quiescing so that nbd_client_receive_next_request() won't start any new request coroutines. Then nbd_drained_poll() tries to makes sure that we wait for any existing request coroutines by checking that client->nb_requests has become 0. However, there is a small window between creating a new request coroutine and increasing client->nb_requests. If a coroutine is in this state, it won't be waited for and drain returns too early. In the context of switching to a different AioContext, this means that blk_aio_attached() will see client->recv_coroutine !=3D NULL and fail its assertion. Fix this by increasing client->nb_requests immediately when starting the coroutine. Doing this after the checks if we should create a new coroutine is okay because client->lock is held. Cc: qemu-stable@nongnu.org Fixes: fd6afc501a01 ("nbd/server: Use drained block ops to quiesce the serv= er") Signed-off-by: Kevin Wolf Message-ID: <20240314165825.40261-2-kwolf@redhat.com> Signed-off-by: Kevin Wolf --- nbd/server.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 941832f178..c3484cc1eb 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -3007,8 +3007,8 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, /* Owns a reference to the NBDClient passed as opaque. */ static coroutine_fn void nbd_trip(void *opaque) { - NBDClient *client =3D opaque; - NBDRequestData *req =3D NULL; + NBDRequestData *req =3D opaque; + NBDClient *client =3D req->client; NBDRequest request =3D { 0 }; /* GCC thinks it can be used uninitia= lized */ int ret; Error *local_err =3D NULL; @@ -3037,8 +3037,6 @@ static coroutine_fn void nbd_trip(void *opaque) goto done; } =20 - req =3D nbd_request_get(client); - /* * nbd_co_receive_request() returns -EAGAIN when nbd_drained_begin() h= as * set client->quiescing but by the time we get back nbd_drained_end()= may @@ -3112,9 +3110,7 @@ static coroutine_fn void nbd_trip(void *opaque) } =20 done: - if (req) { - nbd_request_put(req); - } + nbd_request_put(req); =20 qemu_mutex_unlock(&client->lock); =20 @@ -3143,10 +3139,13 @@ disconnect: */ static void nbd_client_receive_next_request(NBDClient *client) { + NBDRequestData *req; + if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS = && !client->quiescing) { nbd_client_get(client); - client->recv_coroutine =3D qemu_coroutine_create(nbd_trip, client); + req =3D nbd_request_get(client); + client->recv_coroutine =3D qemu_coroutine_create(nbd_trip, req); aio_co_schedule(client->exp->common.ctx, client->recv_coroutine); } } --=20 2.44.0