From nobody Sun May 19 15:58:46 2024 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1588254879; cv=none; d=zohomail.com; s=zohoarc; b=RQb9B6RkNTWRrNbZWl1zZXYm781f8k3xH/Cdk28dTSMBPxIdsMcgXFHqtRe0PH/YGbhQHMmp33geKpOVwvtezJIKogX5CxT/9Er9b2sUQos7/vGLpFFQ4+SL18VPdQxlAyhL2YVY2c8XYxD2kHlMyB1IzurrAiCSlwtcD+VDSC4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588254879; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=B7N/2dA22AZAvZEuIRWbfaStgxBZh00/dxecx4k5Xoo=; b=EJKGjucxiRh5zg8NZ11BS9tZuTfuYsAWLSchWHiUEHucYRQWOPQ7TYup0llyE45Jkk8qMGhWNzWhQR5gj7h7LYwTwALRUmvOyCZz5L53+Iw/bTTU6lEgU7esuq3CoJOf+dUCLI8Ar6Sh/6YQ7hlaGPPEymqExQLWnx2QptskN6o= 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+importer=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 1588254879831232.65899195870566; Thu, 30 Apr 2020 06:54:39 -0700 (PDT) Received: from localhost ([::1]:60528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9eA-00022s-DK for importer@patchew.org; Thu, 30 Apr 2020 09:54:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49826) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9TM-00019i-7g for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:45:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU9Qb-0008Ej-NV for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:43:27 -0400 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:56242) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU9N8-0001RR-1D; Thu, 30 Apr 2020 09:37:02 -0400 Received: from mxbackcorp1g.mail.yandex.net (mxbackcorp1g.mail.yandex.net [IPv6:2a02:6b8:0:1402::301]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 856AF2E148F; Thu, 30 Apr 2020 16:36:58 +0300 (MSK) Received: from myt4-18a966dbd9be.qloud-c.yandex.net (myt4-18a966dbd9be.qloud-c.yandex.net [2a02:6b8:c00:12ad:0:640:18a9:66db]) by mxbackcorp1g.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id 7i37znqff2-atAa7PnK; Thu, 30 Apr 2020 16:36:58 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b080:7108::1:9]) by myt4-18a966dbd9be.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id tZ29y7t6r8-atWKEcqN; Thu, 30 Apr 2020 16:36:55 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1588253818; bh=B7N/2dA22AZAvZEuIRWbfaStgxBZh00/dxecx4k5Xoo=; h=In-Reply-To:In-Reply-To:Message-Id:References:References:Date: Subject:To:From:Cc; b=HGH8myxN3Y/63O9Kv/epJ9XSlQxTpgYIikgVtdc4m8IsGLb6RskZSa/DqTlBHc4kL 7UslRRq+pS0Cnp4VFDOe76Ufs6eX4HgQE+05moAk/7iFfchULsHMjuhOEEvapytVou z0OEBCfcKyWl1qCY2H4Hz7yOhSOvDM370odm/Z4E= Authentication-Results: mxbackcorp1g.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Dima Stepanov To: qemu-devel@nongnu.org Subject: [PATCH v2 1/5] char-socket: return -1 in case of disconnect during tcp_chr_write Date: Thu, 30 Apr 2020 16:36:16 +0300 Message-Id: <2f783e19d2bfe75946ec677ddd5e979f9a3683d2.1588252862.git.dimastep@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: 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=5.45.199.163; envelope-from=dimastep@yandex-team.ru; helo=forwardcorp1j.mail.yandex.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 09:36:59 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 5.45.199.163 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, yc-core@yandex-team.ru, qemu-block@nongnu.org, mst@redhat.com, jasowang@redhat.com, dgilbert@redhat.com, mreitz@redhat.com, arei.gonglei@huawei.com, fengli@smartx.com, stefanha@redhat.com, marcandre.lureau@redhat.com, pbonzini@redhat.com, raphael.norwitz@nutanix.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" During testing of the vhost-user-blk reconnect functionality the qemu SIGSEGV was triggered: start qemu as: x86_64-softmmu/qemu-system-x86_64 -m 1024M -M q35 \ -object memory-backend-file,id=3Dram-node0,size=3D1024M,mem-path=3D/dev/= shm/qemu,share=3Don \ -numa node,cpus=3D0,memdev=3Dram-node0 \ -chardev socket,id=3Dchardev0,path=3D./vhost.sock,noserver,reconnect=3D1= \ -device vhost-user-blk-pci,chardev=3Dchardev0,num-queues=3D4 --enable-kvm start vhost-user-blk daemon: ./vhost-user-blk -s ./vhost.sock -b test-img.raw If vhost-user-blk will be killed during the vhost initialization process, for instance after getting VHOST_SET_VRING_CALL command, then QEMU will fail with the following backtrace: Thread 1 "qemu-system-x86" received signal SIGSEGV, Segmentation fault. 0x00005555559272bb in vhost_user_read (dev=3D0x7fffef2d53e0, msg=3D0x7fffff= ffd5b0) at ./hw/virtio/vhost-user.c:260 260 CharBackend *chr =3D u->user->chr; #0 0x00005555559272bb in vhost_user_read (dev=3D0x7fffef2d53e0, msg=3D0x7= fffffffd5b0) at ./hw/virtio/vhost-user.c:260 #1 0x000055555592acb8 in vhost_user_get_config (dev=3D0x7fffef2d53e0, con= fig=3D0x7fffef2d5394 "", config_len=3D60) at ./hw/virtio/vhost-user.c:1645 #2 0x0000555555925525 in vhost_dev_get_config (hdev=3D0x7fffef2d53e0, con= fig=3D0x7fffef2d5394 "", config_len=3D60) at ./hw/virtio/vhost.c:1490 #3 0x00005555558cc46b in vhost_user_blk_device_realize (dev=3D0x7fffef2d5= 1a0, errp=3D0x7fffffffd8f0) at ./hw/block/vhost-user-blk.c:429 #4 0x0000555555920090 in virtio_device_realize (dev=3D0x7fffef2d51a0, err= p=3D0x7fffffffd948) at ./hw/virtio/virtio.c:3615 #5 0x0000555555a9779c in device_set_realized (obj=3D0x7fffef2d51a0, value= =3Dtrue, errp=3D0x7fffffffdb88) at ./hw/core/qdev.c:891 ... The problem is that vhost_user_write doesn't get an error after disconnect and try to call vhost_user_read(). The tcp_chr_write() routine should return -1 in case of disconnect. Indicate the EIO error if this routine is called in the disconnected state. Signed-off-by: Dima Stepanov Reviewed-by: Marc-Andr=C3=A9 Lureau --- chardev/char-socket.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 185fe38..c128cca 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -175,14 +175,16 @@ static int tcp_chr_write(Chardev *chr, const uint8_t = *buf, int len) if (ret < 0 && errno !=3D EAGAIN) { if (tcp_chr_read_poll(chr) <=3D 0) { tcp_chr_disconnect_locked(chr); - return len; + /* Return an error since we made a disconnect. */ + return ret; } /* else let the read handler finish it properly */ } =20 return ret; } else { - /* XXX: indicate an error ? */ - return len; + /* Indicate an error. */ + errno =3D EIO; + return -1; } } =20 --=20 2.7.4 From nobody Sun May 19 15:58:46 2024 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1588254537; cv=none; d=zohomail.com; s=zohoarc; b=EfzT6xTxq2au7kN2k5O+EzCEK/d4QWGYGJyp6unL5BAzOAa4j3dCr4tz2zqdySY0XPXW2khT2RnnIXtatHDpP4I2/KisgNO+4Zv/oGDykEHmlh2bvzAQbfqkmkEdfdnXybMOs2Mv4YrKKXDiC80+0ZoX5+XfvnZqBOdc8bixVcA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588254537; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=/03BgTwU/psEMUgiBdmqNYh7VlIfn2pdgG96rmbifE4=; b=aZIISnEkbOcou2eyIPANJTL3DJUJgzuHa1FIgBtHNWFTY9l++PYk9PLNry5ftygEoZilmblc2ChXobPV3qWdbBCo0Grjyzl/QJG4zI1wmH+IXxYIHHXm3DN+btgjJJWwFI0SmdFSVkPv/BAXq7kFI2AW34y3Ch0ilFMVTwleEDc= 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+importer=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 1588254537161863.4353780849332; Thu, 30 Apr 2020 06:48:57 -0700 (PDT) Received: from localhost ([::1]:42382 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9Yd-0008Du-OO for importer@patchew.org; Thu, 30 Apr 2020 09:48:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49864) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9TO-0001GZ-I7 for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:45:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU9Qg-0008O5-OE for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:43:30 -0400 Received: from forwardcorp1o.mail.yandex.net ([2a02:6b8:0:1a2d::193]:38496) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU9NG-0001aL-Me; Thu, 30 Apr 2020 09:37:11 -0400 Received: from mxbackcorp1g.mail.yandex.net (mxbackcorp1g.mail.yandex.net [IPv6:2a02:6b8:0:1402::301]) by forwardcorp1o.mail.yandex.net (Yandex) with ESMTP id A73662E1528; Thu, 30 Apr 2020 16:37:05 +0300 (MSK) Received: from myt4-18a966dbd9be.qloud-c.yandex.net (myt4-18a966dbd9be.qloud-c.yandex.net [2a02:6b8:c00:12ad:0:640:18a9:66db]) by mxbackcorp1g.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id i5XRSmKzqP-b2AOJeXr; Thu, 30 Apr 2020 16:37:05 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b080:7108::1:9]) by myt4-18a966dbd9be.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id tZ29y7t6r8-b1WKD6Lc; Thu, 30 Apr 2020 16:37:02 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1588253825; bh=/03BgTwU/psEMUgiBdmqNYh7VlIfn2pdgG96rmbifE4=; h=In-Reply-To:In-Reply-To:Message-Id:References:References:Date: Subject:To:From:Cc; b=XHuZ54R0Z0HFtcKp2BmsBa0NokPj2mOgVdAdrsxmm4A6cb3jiIO0BEeQh2FmEJQbz FP/Zp7phovJE3m+6LhNJYmoS4CghcfP9k1Qvpd6nzcA5S9dLfZ1tjSaw++069fjo9H rKqoEUCykxKJLJhdPt2be/MI7EtFCD5PDkaH0fVg= Authentication-Results: mxbackcorp1g.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Dima Stepanov To: qemu-devel@nongnu.org Subject: [PATCH v2 2/5] vhost: introduce wrappers to set guest notifiers for virtio device Date: Thu, 30 Apr 2020 16:36:17 +0300 Message-Id: <70215284d3e5e39a7f195fac2af97963b00f8173.1588252862.git.dimastep@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: 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=2a02:6b8:0:1a2d::193; envelope-from=dimastep@yandex-team.ru; helo=forwardcorp1o.mail.yandex.net X-detected-operating-system: by eggs.gnu.org: Error: [-] PROGRAM ABORT : Malformed IPv6 address (bad octet value). Location : parse_addr6(), p0f-client.c:67 X-Received-From: 2a02:6b8:0:1a2d::193 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, yc-core@yandex-team.ru, qemu-block@nongnu.org, mst@redhat.com, jasowang@redhat.com, dgilbert@redhat.com, mreitz@redhat.com, arei.gonglei@huawei.com, fengli@smartx.com, stefanha@redhat.com, marcandre.lureau@redhat.com, pbonzini@redhat.com, raphael.norwitz@nutanix.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Introduce new wrappers to set/reset guest notifiers for the virtio device in the vhost device module: vhost_dev_assign_guest_notifiers ->set_guest_notifiers(..., ..., true); vhost_dev_drop_guest_notifiers ->set_guest_notifiers(..., ..., false); This is a preliminary step to refactor code, so the set_guest_notifiers methods could be called based on the vhost device state. Update all vhost used devices to use these wrappers instead of direct method call. Signed-off-by: Dima Stepanov --- backends/cryptodev-vhost.c | 26 +++++++++++++++----------- backends/vhost-user.c | 16 +++++----------- hw/block/vhost-user-blk.c | 15 +++++---------- hw/net/vhost_net.c | 30 +++++++++++++++++------------- hw/scsi/vhost-scsi-common.c | 15 +++++---------- hw/virtio/vhost-user-fs.c | 17 +++++++---------- hw/virtio/vhost-vsock.c | 18 ++++++++---------- hw/virtio/vhost.c | 38 ++++++++++++++++++++++++++++++++++++++ hw/virtio/virtio.c | 13 +++++++++++++ include/hw/virtio/vhost.h | 4 ++++ include/hw/virtio/virtio.h | 1 + 11 files changed, 118 insertions(+), 75 deletions(-) diff --git a/backends/cryptodev-vhost.c b/backends/cryptodev-vhost.c index 8337c9a..4522195 100644 --- a/backends/cryptodev-vhost.c +++ b/backends/cryptodev-vhost.c @@ -169,16 +169,13 @@ vhost_set_vring_enable(CryptoDevBackendClient *cc, int cryptodev_vhost_start(VirtIODevice *dev, int total_queues) { VirtIOCrypto *vcrypto =3D VIRTIO_CRYPTO(dev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(dev))); - VirtioBusState *vbus =3D VIRTIO_BUS(qbus); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(vbus); int r, e; int i; CryptoDevBackend *b =3D vcrypto->cryptodev; CryptoDevBackendVhost *vhost_crypto; CryptoDevBackendClient *cc; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(dev)) { error_report("binding does not support guest notifiers"); return -ENOSYS; } @@ -198,9 +195,13 @@ int cryptodev_vhost_start(VirtIODevice *dev, int total= _queues) } } =20 - r =3D k->set_guest_notifiers(qbus->parent, total_queues, true); + /* + * Since all the states are handled by one vhost device, + * use the first one in array. + */ + vhost_crypto =3D cryptodev_get_vhost(b->conf.peers.ccs[0], b, 0); + r =3D vhost_dev_assign_guest_notifiers(&vhost_crypto->dev, dev, total_= queues); if (r < 0) { - error_report("error binding guest notifier: %d", -r); goto err; } =20 @@ -232,7 +233,8 @@ err_start: vhost_crypto =3D cryptodev_get_vhost(cc, b, i); cryptodev_vhost_stop_one(vhost_crypto, dev); } - e =3D k->set_guest_notifiers(qbus->parent, total_queues, false); + vhost_crypto =3D cryptodev_get_vhost(b->conf.peers.ccs[0], b, 0); + e =3D vhost_dev_drop_guest_notifiers(&vhost_crypto->dev, dev, total_qu= eues); if (e < 0) { error_report("vhost guest notifier cleanup failed: %d", e); } @@ -242,9 +244,6 @@ err: =20 void cryptodev_vhost_stop(VirtIODevice *dev, int total_queues) { - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(dev))); - VirtioBusState *vbus =3D VIRTIO_BUS(qbus); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(vbus); VirtIOCrypto *vcrypto =3D VIRTIO_CRYPTO(dev); CryptoDevBackend *b =3D vcrypto->cryptodev; CryptoDevBackendVhost *vhost_crypto; @@ -259,7 +258,12 @@ void cryptodev_vhost_stop(VirtIODevice *dev, int total= _queues) cryptodev_vhost_stop_one(vhost_crypto, dev); } =20 - r =3D k->set_guest_notifiers(qbus->parent, total_queues, false); + /* + * Since all the states are handled by one vhost device, + * use the first one in array. + */ + vhost_crypto =3D cryptodev_get_vhost(b->conf.peers.ccs[0], b, 0); + r =3D vhost_dev_drop_guest_notifiers(&vhost_crypto->dev, dev, total_qu= eues); if (r < 0) { error_report("vhost guest notifier cleanup failed: %d", r); } diff --git a/backends/vhost-user.c b/backends/vhost-user.c index 2bf3406..e116bc6 100644 --- a/backends/vhost-user.c +++ b/backends/vhost-user.c @@ -60,15 +60,13 @@ vhost_user_backend_dev_init(VhostUserBackend *b, VirtIO= Device *vdev, void vhost_user_backend_start(VhostUserBackend *b) { - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(b->vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret, i ; =20 if (b->started) { return; } =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(b->vdev)) { error_report("binding does not support guest notifiers"); return; } @@ -78,9 +76,8 @@ vhost_user_backend_start(VhostUserBackend *b) return; } =20 - ret =3D k->set_guest_notifiers(qbus->parent, b->dev.nvqs, true); + ret =3D vhost_dev_assign_guest_notifiers(&b->dev, b->vdev, b->dev.nvqs= ); if (ret < 0) { - error_report("Error binding guest notifier"); goto err_host_notifiers; } =20 @@ -104,7 +101,7 @@ vhost_user_backend_start(VhostUserBackend *b) return; =20 err_guest_notifiers: - k->set_guest_notifiers(qbus->parent, b->dev.nvqs, false); + vhost_dev_drop_guest_notifiers(&b->dev, b->vdev, b->dev.nvqs); err_host_notifiers: vhost_dev_disable_notifiers(&b->dev, b->vdev); } @@ -112,8 +109,6 @@ err_host_notifiers: void vhost_user_backend_stop(VhostUserBackend *b) { - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(b->vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret =3D 0; =20 if (!b->started) { @@ -122,9 +117,8 @@ vhost_user_backend_stop(VhostUserBackend *b) =20 vhost_dev_stop(&b->dev, b->vdev); =20 - if (k->set_guest_notifiers) { - ret =3D k->set_guest_notifiers(qbus->parent, - b->dev.nvqs, false); + if (virtio_device_guest_notifiers_initialized(b->vdev)) { + ret =3D vhost_dev_drop_guest_notifiers(&b->dev, b->vdev, b->dev.nv= qs); if (ret < 0) { error_report("vhost guest notifier cleanup failed: %d", ret); } diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c index 17df533..70d7842 100644 --- a/hw/block/vhost-user-blk.c +++ b/hw/block/vhost-user-blk.c @@ -109,11 +109,9 @@ const VhostDevConfigOps blk_ops =3D { static int vhost_user_blk_start(VirtIODevice *vdev) { VHostUserBlk *s =3D VHOST_USER_BLK(vdev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int i, ret; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { error_report("binding does not support guest notifiers"); return -ENOSYS; } @@ -124,9 +122,8 @@ static int vhost_user_blk_start(VirtIODevice *vdev) return ret; } =20 - ret =3D k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); + ret =3D vhost_dev_assign_guest_notifiers(&s->dev, vdev, s->dev.nvqs); if (ret < 0) { - error_report("Error binding guest notifier: %d", -ret); goto err_host_notifiers; } =20 @@ -163,7 +160,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev) return ret; =20 err_guest_notifiers: - k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); + vhost_dev_drop_guest_notifiers(&s->dev, vdev, s->dev.nvqs); err_host_notifiers: vhost_dev_disable_notifiers(&s->dev, vdev); return ret; @@ -172,17 +169,15 @@ err_host_notifiers: static void vhost_user_blk_stop(VirtIODevice *vdev) { VHostUserBlk *s =3D VHOST_USER_BLK(vdev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { return; } =20 vhost_dev_stop(&s->dev, vdev); =20 - ret =3D k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); + ret =3D vhost_dev_drop_guest_notifiers(&s->dev, vdev, s->dev.nvqs); if (ret < 0) { error_report("vhost guest notifier cleanup failed: %d", ret); return; diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index 6b82803..c13b444 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -303,19 +303,15 @@ static void vhost_net_stop_one(struct vhost_net *net, int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, int total_queues) { - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(dev))); - VirtioBusState *vbus =3D VIRTIO_BUS(qbus); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(vbus); + struct vhost_net *net; int r, e, i; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(dev)) { error_report("binding does not support guest notifiers"); return -ENOSYS; } =20 for (i =3D 0; i < total_queues; i++) { - struct vhost_net *net; - net =3D get_vhost_net(ncs[i].peer); vhost_net_set_vq_index(net, i * 2); =20 @@ -328,9 +324,13 @@ int vhost_net_start(VirtIODevice *dev, NetClientState = *ncs, } } =20 - r =3D k->set_guest_notifiers(qbus->parent, total_queues * 2, true); + /* + * Since all the states are handled by one vhost_net device, + * use the first one in array. + */ + net =3D get_vhost_net(ncs[0].peer); + r =3D vhost_dev_assign_guest_notifiers(&net->dev, dev, total_queues * = 2); if (r < 0) { - error_report("Error binding guest notifier: %d", -r); goto err; } =20 @@ -357,7 +357,8 @@ err_start: while (--i >=3D 0) { vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); } - e =3D k->set_guest_notifiers(qbus->parent, total_queues * 2, false); + net =3D get_vhost_net(ncs[0].peer); + e =3D vhost_dev_drop_guest_notifiers(&net->dev, dev, total_queues * 2); if (e < 0) { fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e); fflush(stderr); @@ -369,16 +370,19 @@ err: void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, int total_queues) { - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(dev))); - VirtioBusState *vbus =3D VIRTIO_BUS(qbus); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(vbus); + struct vhost_net *net; int i, r; =20 for (i =3D 0; i < total_queues; i++) { vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); } =20 - r =3D k->set_guest_notifiers(qbus->parent, total_queues * 2, false); + /* + * Since all the states are handled by one vhost_net device, + * use the first one in array. + */ + net =3D get_vhost_net(ncs[0].peer); + r =3D vhost_dev_drop_guest_notifiers(&net->dev, dev, total_queues * 2); if (r < 0) { fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); fflush(stderr); diff --git a/hw/scsi/vhost-scsi-common.c b/hw/scsi/vhost-scsi-common.c index 8ec49d7..8f51ec0 100644 --- a/hw/scsi/vhost-scsi-common.c +++ b/hw/scsi/vhost-scsi-common.c @@ -29,10 +29,8 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) { int ret, i; VirtIODevice *vdev =3D VIRTIO_DEVICE(vsc); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { error_report("binding does not support guest notifiers"); return -ENOSYS; } @@ -42,9 +40,8 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) return ret; } =20 - ret =3D k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true); + ret =3D vhost_dev_assign_guest_notifiers(&vsc->dev, vdev, vsc->dev.nvq= s); if (ret < 0) { - error_report("Error binding guest notifier"); goto err_host_notifiers; } =20 @@ -66,7 +63,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) return ret; =20 err_guest_notifiers: - k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); + vhost_dev_drop_guest_notifiers(&vsc->dev, vdev, vsc->dev.nvqs); err_host_notifiers: vhost_dev_disable_notifiers(&vsc->dev, vdev); return ret; @@ -75,14 +72,12 @@ err_host_notifiers: void vhost_scsi_common_stop(VHostSCSICommon *vsc) { VirtIODevice *vdev =3D VIRTIO_DEVICE(vsc); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret =3D 0; =20 vhost_dev_stop(&vsc->dev, vdev); =20 - if (k->set_guest_notifiers) { - ret =3D k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); + if (virtio_device_guest_notifiers_initialized(vdev)) { + ret =3D vhost_dev_drop_guest_notifiers(&vsc->dev, vdev, vsc->dev.n= vqs); if (ret < 0) { error_report("vhost guest notifier cleanup failed: %d", re= t); } diff --git a/hw/virtio/vhost-user-fs.c b/hw/virtio/vhost-user-fs.c index 6136768..6b101fc 100644 --- a/hw/virtio/vhost-user-fs.c +++ b/hw/virtio/vhost-user-fs.c @@ -38,12 +38,10 @@ static void vuf_get_config(VirtIODevice *vdev, uint8_t = *config) static void vuf_start(VirtIODevice *vdev) { VHostUserFS *fs =3D VHOST_USER_FS(vdev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret; int i; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { error_report("binding does not support guest notifiers"); return; } @@ -54,9 +52,9 @@ static void vuf_start(VirtIODevice *vdev) return; } =20 - ret =3D k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true); + ret =3D vhost_dev_assign_guest_notifiers(&fs->vhost_dev, vdev, + fs->vhost_dev.nvqs); if (ret < 0) { - error_report("Error binding guest notifier: %d", -ret); goto err_host_notifiers; } =20 @@ -79,7 +77,7 @@ static void vuf_start(VirtIODevice *vdev) return; =20 err_guest_notifiers: - k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false); + vhost_dev_drop_guest_notifiers(&fs->vhost_dev, vdev, fs->vhost_dev.nvq= s); err_host_notifiers: vhost_dev_disable_notifiers(&fs->vhost_dev, vdev); } @@ -87,17 +85,16 @@ err_host_notifiers: static void vuf_stop(VirtIODevice *vdev) { VHostUserFS *fs =3D VHOST_USER_FS(vdev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { return; } =20 vhost_dev_stop(&fs->vhost_dev, vdev); =20 - ret =3D k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false= ); + ret =3D vhost_dev_drop_guest_notifiers(&fs->vhost_dev, vdev, + fs->vhost_dev.nvqs); if (ret < 0) { error_report("vhost guest notifier cleanup failed: %d", ret); return; diff --git a/hw/virtio/vhost-vsock.c b/hw/virtio/vhost-vsock.c index 09b6b07..52489dd 100644 --- a/hw/virtio/vhost-vsock.c +++ b/hw/virtio/vhost-vsock.c @@ -75,12 +75,10 @@ static int vhost_vsock_set_running(VHostVSock *vsock, i= nt start) static void vhost_vsock_start(VirtIODevice *vdev) { VHostVSock *vsock =3D VHOST_VSOCK(vdev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret; int i; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { error_report("binding does not support guest notifiers"); return; } @@ -91,9 +89,9 @@ static void vhost_vsock_start(VirtIODevice *vdev) return; } =20 - ret =3D k->set_guest_notifiers(qbus->parent, vsock->vhost_dev.nvqs, tr= ue); + ret =3D vhost_dev_assign_guest_notifiers(&vsock->vhost_dev, + vdev, vsock->vhost_dev.nvqs); if (ret < 0) { - error_report("Error binding guest notifier: %d", -ret); goto err_host_notifiers; } =20 @@ -123,7 +121,8 @@ static void vhost_vsock_start(VirtIODevice *vdev) err_dev_start: vhost_dev_stop(&vsock->vhost_dev, vdev); err_guest_notifiers: - k->set_guest_notifiers(qbus->parent, vsock->vhost_dev.nvqs, false); + vhost_dev_drop_guest_notifiers(&vsock->vhost_dev, + vdev, vsock->vhost_dev.nvqs); err_host_notifiers: vhost_dev_disable_notifiers(&vsock->vhost_dev, vdev); } @@ -131,11 +130,9 @@ err_host_notifiers: static void vhost_vsock_stop(VirtIODevice *vdev) { VHostVSock *vsock =3D VHOST_VSOCK(vdev); - BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); - VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret; =20 - if (!k->set_guest_notifiers) { + if (!virtio_device_guest_notifiers_initialized(vdev)) { return; } =20 @@ -147,7 +144,8 @@ static void vhost_vsock_stop(VirtIODevice *vdev) =20 vhost_dev_stop(&vsock->vhost_dev, vdev); =20 - ret =3D k->set_guest_notifiers(qbus->parent, vsock->vhost_dev.nvqs, fa= lse); + ret =3D vhost_dev_drop_guest_notifiers(&vsock->vhost_dev, + vdev, vsock->vhost_dev.nvqs); if (ret < 0) { error_report("vhost guest notifier cleanup failed: %d", ret); return; diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 01ebe12..fa3da9c 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1419,6 +1419,44 @@ void vhost_dev_disable_notifiers(struct vhost_dev *h= dev, VirtIODevice *vdev) virtio_device_release_ioeventfd(vdev); } =20 +/* + * Assign guest notifiers. + * Should be called after vhost_dev_enable_notifiers. + */ +int vhost_dev_assign_guest_notifiers(struct vhost_dev *hdev, + VirtIODevice *vdev, int nvqs) +{ + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + int ret; + + ret =3D k->set_guest_notifiers(qbus->parent, nvqs, true); + if (ret < 0) { + error_report("Error binding guest notifier: %d", -ret); + } + + return ret; +} + +/* + * Drop guest notifiers. + * Should be called before vhost_dev_disable_notifiers. + */ +int vhost_dev_drop_guest_notifiers(struct vhost_dev *hdev, + VirtIODevice *vdev, int nvqs) +{ + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + int ret; + + ret =3D k->set_guest_notifiers(qbus->parent, nvqs, false); + if (ret < 0) { + error_report("Error reset guest notifier: %d", -ret); + } + + return ret; +} + /* Test and clear event pending status. * Should be called after unmask to avoid losing events. */ diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index b6c8ef5..8a95618 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3812,6 +3812,19 @@ bool virtio_device_ioeventfd_enabled(VirtIODevice *v= dev) return virtio_bus_ioeventfd_enabled(vbus); } =20 +/* + * Check if set_guest_notifiers() method is set by the init routine. + * Return true if yes, otherwise return false. + */ +bool virtio_device_guest_notifiers_initialized(VirtIODevice *vdev) +{ + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + + return k->set_guest_notifiers; +} + + static const TypeInfo virtio_device_info =3D { .name =3D TYPE_VIRTIO_DEVICE, .parent =3D TYPE_DEVICE, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 085450c..4d0d2e2 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -100,6 +100,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevi= ce *vdev); void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev); int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vde= v); +int vhost_dev_assign_guest_notifiers(struct vhost_dev *hdev, + VirtIODevice *vdev, int nvqs); +int vhost_dev_drop_guest_notifiers(struct vhost_dev *hdev, + VirtIODevice *vdev, int nvqs); =20 /* Test and clear masked event pending status. * Should be called after unmask to avoid losing events. diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index b69d517..d9a3d72 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -323,6 +323,7 @@ void virtio_queue_aio_set_host_notifier_handler(VirtQue= ue *vq, AioContext *ctx, VirtIOHandleAIOOutput hand= le_output); VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector); VirtQueue *virtio_vector_next_queue(VirtQueue *vq); +bool virtio_device_guest_notifiers_initialized(VirtIODevice *vdev); =20 static inline void virtio_add_feature(uint64_t *features, unsigned int fbi= t) { --=20 2.7.4 From nobody Sun May 19 15:58:46 2024 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1588255000; cv=none; d=zohomail.com; s=zohoarc; b=UqkHKsRip50Nf3ac7Vx2Zdujk5MdiFsq6BWSCR1PGWqLk9JqWumi0xTat/V4U79/nhLjHGeMMp8QqwL9nF8bVikB6xwJWo/I5WUCmWX1GSukV27qGErqsPsSAovdaMdH4ycY9YfYCBbbc/xPE69RP18YQM6QSWGaOz4r0oM9vQQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588255000; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=kUkFhpkmkerAUcEO23m978mFXGVV+mbiJpdw3pimI38=; b=dUBw817fU1Dy/OnF9bECML0ui4JPdUq/KKvOjPTzUqOI80amBX4Qnt7n+5+EtqvlL7l/+bm469wvYYGSEEEjdCN3PtRGspznw9QtomnK8tMo/ElkzEweZPBxA6CJzaxV+qwOmyGhaJ9Lt+X2m52uwxKJSv6DJekurn3PEbrl1+0= 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+importer=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 158825500091251.943833138741525; Thu, 30 Apr 2020 06:56:40 -0700 (PDT) Received: from localhost ([::1]:41170 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9g7-0006Po-KA for importer@patchew.org; Thu, 30 Apr 2020 09:56:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49836) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9TM-0001BU-Qb for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:45:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU9Qd-0008Fi-E8 for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:43:28 -0400 Received: from forwardcorp1p.mail.yandex.net ([2a02:6b8:0:1472:2741:0:8b6:217]:60108) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU9NM-0001jK-CA; Thu, 30 Apr 2020 09:37:16 -0400 Received: from mxbackcorp2j.mail.yandex.net (mxbackcorp2j.mail.yandex.net [IPv6:2a02:6b8:0:1619::119]) by forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id 45D5D2E151A; Thu, 30 Apr 2020 16:37:12 +0300 (MSK) Received: from myt4-18a966dbd9be.qloud-c.yandex.net (myt4-18a966dbd9be.qloud-c.yandex.net [2a02:6b8:c00:12ad:0:640:18a9:66db]) by mxbackcorp2j.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id SUWv4Mec3s-b7X4UTL4; Thu, 30 Apr 2020 16:37:12 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b080:7108::1:9]) by myt4-18a966dbd9be.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id tZ29y7t6r8-b7WKL4mb; Thu, 30 Apr 2020 16:37:07 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1588253832; bh=kUkFhpkmkerAUcEO23m978mFXGVV+mbiJpdw3pimI38=; h=In-Reply-To:In-Reply-To:Message-Id:References:References:Date: Subject:To:From:Cc; b=Qi+NXMT6Ijxlj0zDe0PAUGxzXOWda3qxv7gQmA+ZoORjIW3HqEiIYEnUoqlD4tBPI gHfE4/nmIUufy+22SeuwA1PkVA7JXHZZcphUSW4Zh+95uWDEMjSLvNNi219uV8mOCv ADGOrjjqNJk6TK2hZvdvNiJoh6loNHp9n0nL/yoQ= Authentication-Results: mxbackcorp2j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Dima Stepanov To: qemu-devel@nongnu.org Subject: [PATCH v2 3/5] vhost-user-blk: add mechanism to track the guest notifiers init state Date: Thu, 30 Apr 2020 16:36:18 +0300 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: 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=2a02:6b8:0:1472:2741:0:8b6:217; envelope-from=dimastep@yandex-team.ru; helo=forwardcorp1p.mail.yandex.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 09:37:12 X-ACL-Warn: Detected OS = ??? X-Received-From: 2a02:6b8:0:1472:2741:0:8b6:217 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, yc-core@yandex-team.ru, qemu-block@nongnu.org, mst@redhat.com, jasowang@redhat.com, dgilbert@redhat.com, mreitz@redhat.com, arei.gonglei@huawei.com, fengli@smartx.com, stefanha@redhat.com, marcandre.lureau@redhat.com, pbonzini@redhat.com, raphael.norwitz@nutanix.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In case of the vhost-user devices the daemon can be killed at any moment. Since QEMU supports the reconnet functionality the guest notifiers should be reset and disabled after "disconnect" event. The most issues were found if the "disconnect" event happened during vhost device initialization step. The disconnect event leads to the call of the vhost_dev_cleanup() routine. Which memset to 0 a vhost device structure. Because of this, if device was not started (dev.started =3D=3D false) and the connection is broken, then the set_guest_notifier method will produce assertion error. Also connection can be broken after the dev.started field is set to true. A new notifiers_set field is added to the vhost_dev structure to track the state of the guest notifiers during the initialization process. Signed-off-by: Dima Stepanov --- hw/block/vhost-user-blk.c | 8 ++++---- hw/virtio/vhost.c | 11 +++++++++++ include/hw/virtio/vhost.h | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c index 70d7842..5a3de0f 100644 --- a/hw/block/vhost-user-blk.c +++ b/hw/block/vhost-user-blk.c @@ -175,7 +175,9 @@ static void vhost_user_blk_stop(VirtIODevice *vdev) return; } =20 - vhost_dev_stop(&s->dev, vdev); + if (s->dev.started) { + vhost_dev_stop(&s->dev, vdev); + } =20 ret =3D vhost_dev_drop_guest_notifiers(&s->dev, vdev, s->dev.nvqs); if (ret < 0) { @@ -337,9 +339,7 @@ static void vhost_user_blk_disconnect(DeviceState *dev) } s->connected =3D false; =20 - if (s->dev.started) { - vhost_user_blk_stop(vdev); - } + vhost_user_blk_stop(vdev); =20 vhost_dev_cleanup(&s->dev); } diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index fa3da9c..ddbdc53 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1380,6 +1380,7 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev= , VirtIODevice *vdev) goto fail_vq; } } + hdev->notifiers_set =3D true; =20 return 0; fail_vq: @@ -1407,6 +1408,10 @@ void vhost_dev_disable_notifiers(struct vhost_dev *h= dev, VirtIODevice *vdev) BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); int i, r; =20 + if (!hdev->notifiers_set) { + return; + } + for (i =3D 0; i < hdev->nvqs; ++i) { r =3D virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_inde= x + i, false); @@ -1417,6 +1422,8 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hd= ev, VirtIODevice *vdev) virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index = + i); } virtio_device_release_ioeventfd(vdev); + + hdev->notifiers_set =3D false; } =20 /* @@ -1449,6 +1456,10 @@ int vhost_dev_drop_guest_notifiers(struct vhost_dev = *hdev, VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret; =20 + if (!hdev->notifiers_set) { + return 0; + } + ret =3D k->set_guest_notifiers(qbus->parent, nvqs, false); if (ret < 0) { error_report("Error reset guest notifier: %d", -ret); diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 4d0d2e2..e3711a7 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -90,6 +90,7 @@ struct vhost_dev { QLIST_HEAD(, vhost_iommu) iommu_list; IOMMUNotifier n; const VhostDevConfigOps *config_ops; + bool notifiers_set; }; =20 int vhost_dev_init(struct vhost_dev *hdev, void *opaque, --=20 2.7.4 From nobody Sun May 19 15:58:46 2024 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1588254709; cv=none; d=zohomail.com; s=zohoarc; b=m13Vg7yxkPuMtbBOi6OaQR3ZniFM+2CrPUxCI8bfIS7WHT0grEq3teNNuVUbUIxrFi6z5sKV4MqYHu6XH0mLdbtPwxzGkagyx3MhAfrFUIRldiYcKre8pQaBoawa5rdJxj7RS+rrUwVhiRLlpK/Wt04KE1+fY/O3j12FatvfcK8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588254709; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=5FKUZYsWXSmWGJbzPGJ+rs4aPqD/ORGDojQy3boMTtI=; b=iP8cG167VVORFkkbJfPrRtuT2zpozzVBeUS2D8AZyq9f6XsBQAGlkrApXg3I9D9w4778ekHgWuDcPLlFxxWg5Slb3DQuCbo7nZjX2gBaVCq+YXX8MKEflTNl80sZusW1Ndbucmst/MBiwqmYnQQ2x9XQBm/WRWHXJAZhmXz9tJ8= 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+importer=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 1588254709926754.1565381298819; Thu, 30 Apr 2020 06:51:49 -0700 (PDT) Received: from localhost ([::1]:48698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9bQ-0003be-Ix for importer@patchew.org; Thu, 30 Apr 2020 09:51:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49852) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9TN-0001DL-Cl for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:45:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU9Qe-0008Hn-De for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:43:29 -0400 Received: from forwardcorp1o.mail.yandex.net ([95.108.205.193]:47496) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU9NP-0001td-No; Thu, 30 Apr 2020 09:37:21 -0400 Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1o.mail.yandex.net (Yandex) with ESMTP id 9E9442E14E4; Thu, 30 Apr 2020 16:37:16 +0300 (MSK) Received: from myt4-18a966dbd9be.qloud-c.yandex.net (myt4-18a966dbd9be.qloud-c.yandex.net [2a02:6b8:c00:12ad:0:640:18a9:66db]) by mxbackcorp1j.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id GX0Aiw5eLv-bDWC24Y5; Thu, 30 Apr 2020 16:37:16 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b080:7108::1:9]) by myt4-18a966dbd9be.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id tZ29y7t6r8-bDWKtTr9; Thu, 30 Apr 2020 16:37:13 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1588253836; bh=5FKUZYsWXSmWGJbzPGJ+rs4aPqD/ORGDojQy3boMTtI=; h=In-Reply-To:In-Reply-To:Message-Id:References:References:Date: Subject:To:From:Cc; b=p0fxPmCdL9FwkOg0YTayXa1Va0q3FxG8Y16hf0JDiXp9Hd41Ga21u+uS3i6qQU2GK qb6GWYHYANeAz80Mlgg4x9tDLZzjTA/H3ZzyOGsXhxq0Ni1sq8UgZIjqzC96vdr7cK glGQ17a8sEtQhQO7bML6D2VBoATooMcuIxZu7AZE= Authentication-Results: mxbackcorp1j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Dima Stepanov To: qemu-devel@nongnu.org Subject: [PATCH v2 4/5] vhost: check vring address before calling unmap Date: Thu, 30 Apr 2020 16:36:19 +0300 Message-Id: <2d4952df2cc246f7421b4b9023a581b22210fc41.1588252862.git.dimastep@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: 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=95.108.205.193; envelope-from=dimastep@yandex-team.ru; helo=forwardcorp1o.mail.yandex.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 09:37:16 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 95.108.205.193 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, yc-core@yandex-team.ru, qemu-block@nongnu.org, mst@redhat.com, jasowang@redhat.com, dgilbert@redhat.com, mreitz@redhat.com, arei.gonglei@huawei.com, fengli@smartx.com, stefanha@redhat.com, marcandre.lureau@redhat.com, pbonzini@redhat.com, raphael.norwitz@nutanix.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Since disconnect can happen at any time during initialization not all vring buffers (for instance used vring) can be intialized successfully. If the buffer was not initialized then vhost_memory_unmap call will lead to SIGSEGV. Add checks for the vring address value before calling unmap. Also add assert() in the vhost_memory_unmap() routine. Signed-off-by: Dima Stepanov Reviewed-by: Raphael Norwitz --- hw/virtio/vhost.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index ddbdc53..3ee50c4 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -314,6 +314,8 @@ static void vhost_memory_unmap(struct vhost_dev *dev, v= oid *buffer, hwaddr len, int is_write, hwaddr access_len) { + assert(buffer); + if (!vhost_dev_has_iommu(dev)) { cpu_physical_memory_unmap(buffer, len, is_write, access_len); } @@ -1132,12 +1134,25 @@ static void vhost_virtqueue_stop(struct vhost_dev *= dev, vhost_vq_index); } =20 - vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx= ), - 1, virtio_queue_get_used_size(vdev, idx)); - vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, i= dx), - 0, virtio_queue_get_avail_size(vdev, idx)); - vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx= ), - 0, virtio_queue_get_desc_size(vdev, idx)); + /* + * Since the vhost-user disconnect can happen during initialization + * check if vring was initialized, before making unmap. + */ + if (vq->used) { + vhost_memory_unmap(dev, vq->used, + virtio_queue_get_used_size(vdev, idx), + 1, virtio_queue_get_used_size(vdev, idx)); + } + if (vq->avail) { + vhost_memory_unmap(dev, vq->avail, + virtio_queue_get_avail_size(vdev, idx), + 0, virtio_queue_get_avail_size(vdev, idx)); + } + if (vq->desc) { + vhost_memory_unmap(dev, vq->desc, + virtio_queue_get_desc_size(vdev, idx), + 0, virtio_queue_get_desc_size(vdev, idx)); + } } =20 static void vhost_eventfd_add(MemoryListener *listener, --=20 2.7.4 From nobody Sun May 19 15:58:46 2024 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1588255076; cv=none; d=zohomail.com; s=zohoarc; b=DTnW+nmrkY3q70Dg8ELZsVmrRBsXvg22+ojub6FdoaSmJE4QJxZSWy/m/YtYJ07GSJn/i/cno/GfTfy2NzzGBblT15ellhJHgH/4aS3wW7Bim06Iaog1St0SlQOPxdAtZjP7gOsqS1udPgSMa7eVxAWzWWlLMfaDsONTA8YpNGE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588255076; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=Fv6pZWWviC9Gnz6pDjZHVEUsXW2GPb2wYw220Nlajio=; b=NbzXhFAHI8VYerNdci8XY1jX9/10kvTilXp5ueISMYZMK/hZ+supaNvZV9HIeLBnVIh3t2b8D/NBgWh2vkQqSdNBuMefYC+Kf4XneICNBdtJRaBAWE3NBiHDzv1abMk8DC9yOz4LOq4JtCMg6Q+77P7f2TOKnpONpSXmf65L8WY= 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+importer=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 1588255076931571.2919738434506; Thu, 30 Apr 2020 06:57:56 -0700 (PDT) Received: from localhost ([::1]:46418 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9hL-00005z-IH for importer@patchew.org; Thu, 30 Apr 2020 09:57:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49860) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU9TO-0001FH-2L for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:45:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU9Qh-0008OL-Aq for qemu-devel@nongnu.org; Thu, 30 Apr 2020 09:43:29 -0400 Received: from forwardcorp1o.mail.yandex.net ([2a02:6b8:0:1a2d::193]:38810) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU9NS-00029D-1G; Thu, 30 Apr 2020 09:37:22 -0400 Received: from mxbackcorp1o.mail.yandex.net (mxbackcorp1o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::301]) by forwardcorp1o.mail.yandex.net (Yandex) with ESMTP id 4265E2E1537; Thu, 30 Apr 2020 16:37:20 +0300 (MSK) Received: from myt4-18a966dbd9be.qloud-c.yandex.net (myt4-18a966dbd9be.qloud-c.yandex.net [2a02:6b8:c00:12ad:0:640:18a9:66db]) by mxbackcorp1o.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id UvACH8Lz7U-bIbKtqjR; Thu, 30 Apr 2020 16:37:20 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b080:7108::1:9]) by myt4-18a966dbd9be.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id tZ29y7t6r8-bHWKsw5w; Thu, 30 Apr 2020 16:37:18 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1588253840; bh=Fv6pZWWviC9Gnz6pDjZHVEUsXW2GPb2wYw220Nlajio=; h=In-Reply-To:In-Reply-To:Message-Id:References:References:Date: Subject:To:From:Cc; b=1NtO/HyijYGOZgH8x+JaBvM5ZRZOjnhZi2qMZB/kWqYr8w8oPfh+yrG3GxSyK+ah4 aUiMPOvtUe5J8WJ7Bnhg83hwdkYe9CsrxO842iC9ntjeWnWYYE4yT3xY3+sfXM89wf LjRemFkKUXGN770oVPRN08BIcMiI85UEftYQJPrk= Authentication-Results: mxbackcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Dima Stepanov To: qemu-devel@nongnu.org Subject: [PATCH v2 5/5] vhost: add device started check in migration set log Date: Thu, 30 Apr 2020 16:36:20 +0300 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: 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=2a02:6b8:0:1a2d::193; envelope-from=dimastep@yandex-team.ru; helo=forwardcorp1o.mail.yandex.net X-detected-operating-system: by eggs.gnu.org: Error: [-] PROGRAM ABORT : Malformed IPv6 address (bad octet value). Location : parse_addr6(), p0f-client.c:67 X-Received-From: 2a02:6b8:0:1a2d::193 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, yc-core@yandex-team.ru, qemu-block@nongnu.org, mst@redhat.com, jasowang@redhat.com, dgilbert@redhat.com, mreitz@redhat.com, arei.gonglei@huawei.com, fengli@smartx.com, stefanha@redhat.com, marcandre.lureau@redhat.com, pbonzini@redhat.com, raphael.norwitz@nutanix.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If vhost-user daemon is used as a backend for the vhost device, then we should consider a possibility of disconnect at any moment. If such disconnect happened in the vhost_migration_log() routine the vhost device structure will be clean up. At the start of the vhost_migration_log() function there is a check: if (!dev->started) { dev->log_enabled =3D enable; return 0; } To be consistent with this check add the same check after calling the vhost_dev_set_log() routine. This in general help not to break a migration due the assert() message. But it looks like that this code should be revised to handle these errors more carefully. In case of vhost-user device backend the fail paths should consider the state of the device. In this case we should skip some function calls during rollback on the error paths, so not to get the NULL dereference errors. Signed-off-by: Dima Stepanov --- hw/virtio/vhost.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 3ee50c4..d5ab96d 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -787,6 +787,17 @@ static int vhost_dev_set_features(struct vhost_dev *de= v, static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log) { int r, i, idx; + + if (!dev->started) { + /* + * If vhost-user daemon is used as a backend for the + * device and the connection is broken, then the vhost_dev + * structure will be reset all its values to 0. + * Add additional check for the device state. + */ + return -1; + } + r =3D vhost_dev_set_features(dev, enable_log); if (r < 0) { goto err_features; @@ -801,12 +812,19 @@ static int vhost_dev_set_log(struct vhost_dev *dev, b= ool enable_log) } return 0; err_vq: - for (; i >=3D 0; --i) { + /* + * Disconnect with the vhost-user daemon can lead to the + * vhost_dev_cleanup() call which will clean up vhost_dev + * structure. + */ + for (; dev->started && (i >=3D 0); --i) { idx =3D dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, dev->log_enabled); } - vhost_dev_set_features(dev, dev->log_enabled); + if (dev->started) { + vhost_dev_set_features(dev, dev->log_enabled); + } err_features: return r; } @@ -832,7 +850,15 @@ static int vhost_migration_log(MemoryListener *listene= r, int enable) } else { vhost_dev_log_resize(dev, vhost_get_log_size(dev)); r =3D vhost_dev_set_log(dev, true); - if (r < 0) { + /* + * The dev log resize can fail, because of disconnect + * with the vhost-user-blk daemon. Check the device + * state before calling the vhost_dev_set_log() + * function. + * Don't return error if device isn't started to be + * consistent with the check above. + */ + if (dev->started && r < 0) { return r; } } @@ -1739,7 +1765,12 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODe= vice *vdev) fail_log: vhost_log_put(hdev, false); fail_vq: - while (--i >=3D 0) { + /* + * Disconnect with the vhost-user daemon can lead to the + * vhost_dev_cleanup() call which will clean up vhost_dev + * structure. + */ + while ((--i >=3D 0) && (hdev->started)) { vhost_virtqueue_stop(hdev, vdev, hdev->vqs + i, --=20 2.7.4