From nobody Fri May 17 09:01:37 2024 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=1710435555; cv=none; d=zohomail.com; s=zohoarc; b=meRTJEwdRuxsub4DZ9OlBc3wWKjvKxsq0V93irs4g6fesDWIjYKU/QkG+lf/o/HUlrVWHr0Y2B3M9oxHfyaeTQ7pk5DYDAlbtPL0Mr3Q78UMILAq5ZmFYQdsQs+wPGtKCrJIBtHZwNOairDvDwHE+rzmox+JnGgWS/lbDDi3mjg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710435555; 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=31gUSxdMtfnI8RfpkSr3yYSOm01I+BkSu6LhD9mu5aw=; b=Sii4K0vX2m/CbR3mMYRMInLTcndZAxV//T755R8p/cI03R+RTSBLeQjUJwgdrUsJJ5z3PfSUITA0AgZTSaqcvxMNdKQE87Ug7OV7uA/2xl8npEVefxj+UBxCrQD5RBgyhGyQgAdOJF5JRcn1QGdA43elaEO7ap9EQHbESwNZWUk= 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 1710435555289114.42351089355986; Thu, 14 Mar 2024 09:59:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rkoPu-0006Ri-SY; Thu, 14 Mar 2024 12:58:54 -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 1rkoPp-0006PQ-Dp for qemu-devel@nongnu.org; Thu, 14 Mar 2024 12:58:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rkoPk-0003Z4-MI for qemu-devel@nongnu.org; Thu, 14 Mar 2024 12:58:49 -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-13-aA9h54FHNKSx0T7eS7tCbw-1; Thu, 14 Mar 2024 12:58:39 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 2B2B089C661; Thu, 14 Mar 2024 16:58:39 +0000 (UTC) Received: from merkur.redhat.com (unknown [10.39.194.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id BFE83111E40C; Thu, 14 Mar 2024 16:58:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710435523; 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=31gUSxdMtfnI8RfpkSr3yYSOm01I+BkSu6LhD9mu5aw=; b=BHfuMX8qLseNSdQJw7UACHGtcMybTJ9CCLOUQnRovXwOTMKUwMeTIl+zBsxOEgaagVIs2r rhYRoX83FRK8ktAH/o894W6zLCbApNskg/6GOnQqnHsX9jYfUlx2cIqUQCJrUxSJWoBtwl 6DS5EYxfkF1xBl/pcnUxpzB+8HR/Lhw= X-MC-Unique: aA9h54FHNKSx0T7eS7tCbw-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com, hreitz@redhat.com, eblake@redhat.com, aliang@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH for-9.0 1/2] nbd/server: Fix race in draining the export Date: Thu, 14 Mar 2024 17:58:24 +0100 Message-ID: <20240314165825.40261-2-kwolf@redhat.com> In-Reply-To: <20240314165825.40261-1-kwolf@redhat.com> References: <20240314165825.40261-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.987, 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=unavailable 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: 1710435556968100003 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: fd6afc501a019682d1b8468b562355a2887087bd Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- 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 From nobody Fri May 17 09:01:37 2024 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=1710435567; cv=none; d=zohomail.com; s=zohoarc; b=UQQWEGbCQkIy3llfatrfZp9MCWEBuEiC9llCqOYcDfbQ+vZcWekza1KAUjy0qw6r/3rsS5vXO15FDDWr+xgfW0jxYtCqpxm1BoJt4njibXEqZ+2TN9ZNOKgHpxklIyZMimgaALmVWj2ES3YmsnFJMUZyYPqKLOCfgUm1sg3oD3U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710435567; 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=/+cEmQc9bjBuc+D/nWptCsUwj1Y/K/92w9pm5FPLf5E=; b=EkIlXbIUEsMTbHN8o+70lGE08Pa7FjygQVZ1lNmVAoDGGB07hwDXoGpYsjLAG8tVocmhDvDDO5iDfQfHRGLjyVC0YA2PI8Cj9WPIAzFsfh75CYfX05u6rwsgLeaasBuXw7tcGELF+vRaQc423dwmpQVPIAqWIJiPnplA2DW1we4= 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 1710435567925736.6239004322957; Thu, 14 Mar 2024 09:59:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rkoPt-0006Qj-OX; Thu, 14 Mar 2024 12:58:53 -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 1rkoPm-0006P5-TL for qemu-devel@nongnu.org; Thu, 14 Mar 2024 12:58:48 -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 1rkoPk-0003Ys-Dd for qemu-devel@nongnu.org; Thu, 14 Mar 2024 12:58:46 -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-428-ANGe5-KxPWawvnJd6mA_JA-1; Thu, 14 Mar 2024 12:58:42 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 BCE35101A586; Thu, 14 Mar 2024 16:58:41 +0000 (UTC) Received: from merkur.redhat.com (unknown [10.39.194.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60F8B111E408; Thu, 14 Mar 2024 16:58:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710435523; 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=/+cEmQc9bjBuc+D/nWptCsUwj1Y/K/92w9pm5FPLf5E=; b=eGAjEMNPVtBo5guo/kfnl5LPpTH/stebho82x6rNKozgt3dOE8fZf0fsbuf5Bq+to+890d Pudn1JHe440bL5MPSrQze2jFBSKvdGHczTZ4VsdFB8/PxrZpqgbX7xVjo0nEezCEA3Fa1t wBMt9exqyLzKYPCCIJTPv4zdOXrtOFc= X-MC-Unique: ANGe5-KxPWawvnJd6mA_JA-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com, hreitz@redhat.com, eblake@redhat.com, aliang@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH for-9.0 2/2] iotests: Add test for reset/AioContext switches with NBD exports Date: Thu, 14 Mar 2024 17:58:25 +0100 Message-ID: <20240314165825.40261-3-kwolf@redhat.com> In-Reply-To: <20240314165825.40261-1-kwolf@redhat.com> References: <20240314165825.40261-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.987, 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: 1710435569030100003 Content-Type: text/plain; charset="utf-8" This replicates the scenario in which the bug was reported. Unfortunately this relies on actually executing a guest (so that the firmware initialises the virtio-blk device and moves it to its configured iothread), so this can't make use of the qtest accelerator like most other test cases. I tried to find a different easy way to trigger the bug, but couldn't find one. Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- tests/qemu-iotests/tests/iothreads-nbd-export | 66 +++++++++++++++++++ .../tests/iothreads-nbd-export.out | 19 ++++++ 2 files changed, 85 insertions(+) create mode 100755 tests/qemu-iotests/tests/iothreads-nbd-export create mode 100644 tests/qemu-iotests/tests/iothreads-nbd-export.out diff --git a/tests/qemu-iotests/tests/iothreads-nbd-export b/tests/qemu-iot= ests/tests/iothreads-nbd-export new file mode 100755 index 0000000000..63cac8fdbf --- /dev/null +++ b/tests/qemu-iotests/tests/iothreads-nbd-export @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# group: rw quick +# +# Copyright (C) 2024 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Creator/Owner: Kevin Wolf + +import asyncio +import iotests +import qemu +import time + +iotests.script_initialize(supported_fmts=3D['qcow2'], + supported_platforms=3D['linux']) + +with iotests.FilePath('disk1.img') as path, \ + iotests.FilePath('nbd.sock', base_dir=3Diotests.sock_dir) as nbd_sock= , \ + qemu.machine.QEMUMachine(iotests.qemu_prog) as vm: + + img_size =3D '10M' + + iotests.log('Preparing disk...') + iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size) + vm.add_args('-blockdev', f'file,node-name=3Ddisk-file,filename=3D{path= }') + vm.add_args('-blockdev', f'qcow2,node-name=3Ddisk,file=3Ddisk-file') + vm.add_args('-object', 'iothread,id=3Diothread0') + vm.add_args('-device', 'virtio-blk,drive=3Ddisk,iothread=3Diothread0,s= hare-rw=3Don') + + iotests.log('Launching VM...') + vm.add_args('-accel', 'kvm', '-accel', 'tcg') + #vm.add_args('-accel', 'qtest') + vm.launch() + + iotests.log('Exporting to NBD...') + iotests.log(vm.qmp('nbd-server-start', + addr=3D{'type': 'unix', 'data': {'path': nbd_sock}}= )) + iotests.log(vm.qmp('block-export-add', type=3D'nbd', id=3D'exp0', + node_name=3D'disk', writable=3DTrue)) + + iotests.log('Connecting qemu-img...') + qemu_io =3D iotests.QemuIoInteractive('-f', 'raw', + f'nbd+unix:///disk?socket=3D{nbd_s= ock}') + + iotests.log('Moving the NBD export to a different iothread...') + for i in range(0, 10): + iotests.log(vm.qmp('system_reset')) + time.sleep(0.1) + + iotests.log('Checking that it is still alive...') + iotests.log(vm.qmp('query-status')) + + qemu_io.close() + vm.shutdown() diff --git a/tests/qemu-iotests/tests/iothreads-nbd-export.out b/tests/qemu= -iotests/tests/iothreads-nbd-export.out new file mode 100644 index 0000000000..bc514e35e5 --- /dev/null +++ b/tests/qemu-iotests/tests/iothreads-nbd-export.out @@ -0,0 +1,19 @@ +Preparing disk... +Launching VM... +Exporting to NBD... +{"return": {}} +{"return": {}} +Connecting qemu-img... +Moving the NBD export to a different iothread... +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +Checking that it is still alive... +{"return": {"running": true, "status": "running"}} --=20 2.44.0