From nobody Mon Apr 29 11:59:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1488532038363158.6662703467838; Fri, 3 Mar 2017 01:07:18 -0800 (PST) Received: from localhost ([::1]:56827 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cjjBD-0006qF-5Z for importer@patchew.org; Fri, 03 Mar 2017 04:07:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49274) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cjjAY-0006pF-LL for qemu-devel@nongnu.org; Fri, 03 Mar 2017 04:06:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cjjAV-0007zd-G8 for qemu-devel@nongnu.org; Fri, 03 Mar 2017 04:06:34 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52424) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cjjAV-0007yu-8K for qemu-devel@nongnu.org; Fri, 03 Mar 2017 04:06:31 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5C10F4E04A for ; Fri, 3 Mar 2017 09:06:31 +0000 (UTC) Received: from jason-ThinkPad-T450s.redhat.com (vpn1-5-124.pek2.redhat.com [10.72.5.124]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2396N4v027885; Fri, 3 Mar 2017 04:06:25 -0500 From: Jason Wang To: mst@redhat.com, qemu-devel@nongnu.org Date: Fri, 3 Mar 2017 17:06:20 +0800 Message-Id: <1488531980-13235-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 03 Mar 2017 09:06:31 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] virito: unbreak virtio device behinds IOMMU with region cache X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marcel@redhat.com, Paolo Bonzini , Jason Wang , peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Commit c611c76417f5 ("virtio: add MemoryListener to cache ring translations") registers a memory listener to dma_as. This may not work when IOMMU is enabled: dma_as(bus_master_as) were correctly initialized in pcibus_machine_done() after virtio_realize() where we try to register listener and initialize address space cache. Fixing this by: - delay the listener register to status set - reset dma_as before trying to initialize address spaces to make sure it works even IOMMU were created after virtio device Fixes: c611c76417f5 ("virtio: add MemoryListener to cache ring translations= ") Cc: Paolo Bonzini Signed-off-by: Jason Wang --- hw/virtio/virtio.c | 29 ++++++++++++++++++++++++++++- include/hw/virtio/virtio.h | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 23483c7..179030c 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1084,9 +1084,22 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t va= l) } } } + + virtio_device_reset_dma_as(vdev); + + if (val =3D=3D (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER))= { + memory_listener_unregister(&vdev->listener); + memory_listener_register(&vdev->listener, vdev->dma_as); + } + if (k->set_status) { k->set_status(vdev, val); } + + if (val =3D=3D 0) { + memory_listener_unregister(&vdev->listener); + } + vdev->status =3D val; return 0; } @@ -2402,7 +2415,6 @@ static void virtio_device_realize(DeviceState *dev, E= rror **errp) } =20 vdev->listener.commit =3D virtio_memory_listener_commit; - memory_listener_register(&vdev->listener, vdev->dma_as); } =20 static void virtio_device_unrealize(DeviceState *dev, Error **errp) @@ -2576,6 +2588,21 @@ bool virtio_device_ioeventfd_enabled(VirtIODevice *v= dev) return virtio_bus_ioeventfd_enabled(vbus); } =20 +void virtio_device_reset_dma_as(VirtIODevice *vdev) +{ + DeviceState *qdev =3D DEVICE(vdev); + BusState *qbus =3D BUS(qdev_get_parent_bus(qdev)); + VirtioBusState *bus =3D VIRTIO_BUS(qbus); + VirtioBusClass *klass =3D VIRTIO_BUS_GET_CLASS(bus); + bool has_iommu =3D virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFO= RM); + + if (klass->get_dma_as !=3D NULL && has_iommu) { + vdev->dma_as =3D klass->get_dma_as(qbus->parent); + } else { + vdev->dma_as =3D &address_space_memory; + } +} + static const TypeInfo virtio_device_info =3D { .name =3D TYPE_VIRTIO_DEVICE, .parent =3D TYPE_DEVICE, diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 15efcf2..f7e0b4a 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -289,6 +289,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); +void virtio_device_reset_dma_as(VirtIODevice *vdev); =20 static inline void virtio_add_feature(uint64_t *features, unsigned int fbi= t) { --=20 2.7.4