From nobody Mon Feb 9 21:01:05 2026 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=1587667471; cv=none; d=zohomail.com; s=zohoarc; b=hGSEcfM2uu7NjxExFQWpWzI0Unm00TZsviBUMknXrAGnSmzs1R7iNssMxSbeDmpq9VaMM1VCd7vR3UadKuZ/DeioDZ6O41Aq8KhqOp+pxc7Q3Hdt6lmv1PNpxZanuSbHOV+AdikjnRcBO94Zq866cIanBYd1H9quw7Ww4hQDT08= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587667471; 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=Nh1Yo9YBgR3X9ny+aKKWZYygpxMHiRLEvwfUjEaPOUchqh9pnowJMPLAB+gYtq66KqbLBMTIZpgdbKUfRHqWyNn3RInQzes4v2WC20jj+fJAGno3WS8Dj0wt7Y/WX+CirYV2YIECbFmUZfCZIMocDAT9oGjuLnDfFaYdbtwPb68= 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 1587667471047478.8689483610365; Thu, 23 Apr 2020 11:44:31 -0700 (PDT) Received: from localhost ([::1]:36800 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRgpp-0007HK-MK for importer@patchew.org; Thu, 23 Apr 2020 14:44:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32902) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRgmB-0001Hq-2O for qemu-devel@nongnu.org; Thu, 23 Apr 2020 14:40:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jRgm9-0004W6-Vv for qemu-devel@nongnu.org; Thu, 23 Apr 2020 14:40:42 -0400 Received: from forwardcorp1p.mail.yandex.net ([2a02:6b8:0:1472:2741:0:8b6:217]:46980) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jRgm6-0004EK-Hx; Thu, 23 Apr 2020 14:40:38 -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 46DE32E150B; Thu, 23 Apr 2020 21:40:36 +0300 (MSK) Received: from vla5-58875c36c028.qloud-c.yandex.net (vla5-58875c36c028.qloud-c.yandex.net [2a02:6b8:c18:340b:0:640:5887:5c36]) by mxbackcorp2j.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id WNXL47K6ek-eXHeXm5I; Thu, 23 Apr 2020 21:40:36 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b081:1313::1:e]) by vla5-58875c36c028.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id 7hClKENKMK-eXXeVA3o; Thu, 23 Apr 2020 21:40:33 +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=1587667236; bh=/03BgTwU/psEMUgiBdmqNYh7VlIfn2pdgG96rmbifE4=; h=In-Reply-To:In-Reply-To:Message-Id:References:References:Date: Subject:To:From:Cc; b=yYbBu7FWt+zJ+GVyTZuw4ERXVRJm6Bxpcz84NOQaz5eu9S451HXTVJKPYNxoOez19 Me7mjeDqe9tq7XXBzoiCv3riPhipup/k4JdvHrYer8KXddILJ4/o4sYlb+roPF7xdN xBJ1N+hrWOhdp/U11fbw/Y/e/NN+URRT8EsMVOW0= Authentication-Results: mxbackcorp2j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Dima Stepanov To: qemu-devel@nongnu.org Subject: [RFC PATCH v1 4/7] vhost: introduce wrappers to set guest notifiers for virtio device Date: Thu, 23 Apr 2020 21:39:35 +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/23 14:40:14 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, 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